Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into pr/26
Browse files Browse the repository at this point in the history
# Conflicts:
#	GChan/ImageLink.cs
  • Loading branch information
Issung committed Aug 13, 2023
2 parents ddbc3cf + e9c7c25 commit a142b03
Show file tree
Hide file tree
Showing 18 changed files with 456 additions and 336 deletions.
149 changes: 86 additions & 63 deletions GChan/Controllers/MainController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using GChan.Data;
using GChan.Forms;
using GChan.Helpers;
using GChan.Models;
using GChan.Properties;
using GChan.Trackers;
Expand Down Expand Up @@ -82,7 +83,7 @@ public MainController(MainForm mainForm)

scanTimer.Enabled = false;
scanTimer.Interval = Settings.Default.ScanTimer;
scanTimer.Tick += new EventHandler(Scan);
scanTimer.Tick += new EventHandler(StartScanThread);
}

public void LoadTrackers()
Expand All @@ -106,24 +107,23 @@ public void LoadTrackers()

new SysThread(() =>
{
// END TODO
Parallel.ForEach(threads, (thread) =>
{
Thread newThread = (Thread)Utils.CreateNewTracker(thread);
Form.BeginInvoke(new Action(() => { AddNewTracker(newThread); }));
});

Form.Invoke((MethodInvoker)delegate {
Done();
FinishLoadingTrackers();
});
}).Start();
}

/// Executed once everything has finished being loaded.
void Done()
void FinishLoadingTrackers()
{
scanTimer.Enabled = true;
Scan(this, new EventArgs());
StartScanThread(this, new EventArgs());

// Check for updates.
if (Settings.Default.CheckForUpdatesOnStart)
Expand All @@ -133,41 +133,56 @@ void Done()
}
}

public void AddUrl(string url)
public void AddUrls(IEnumerable<string> urls)
{
Tracker newTracker = Utils.CreateNewTracker(url);

if (newTracker != null)
{
List<Tracker> trackerList = ((newTracker.Type == Type.Board) ? Model.Boards.Cast<Tracker>() : Model.Threads.Cast<Tracker>()).ToList();
bool trackerWasAdded = false;
foreach (var url in urls)
{
var newTracker = Utils.CreateNewTracker(url);

if (IsUnique(newTracker, trackerList))
{
AddNewTracker(newTracker);
scanTimer.Enabled = true;
Scan(this, new EventArgs());
}
else
if (newTracker != null)
{
DialogResult result = MessageBox.Show(
"URL is already being tracked!\nOpen corresponding folder?",
"Error", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
var trackerList = ((newTracker.Type == Type.Board) ? Model.Boards.Cast<Tracker>() : Model.Threads.Cast<Tracker>()).ToArray();

if (result == DialogResult.Yes)
if (IsUnique(newTracker, trackerList))
{
var addSuccess = AddNewTracker(newTracker);
if (addSuccess)
{
trackerWasAdded = true;
}
}
else
{
string spath = newTracker.SaveTo;
if (!Directory.Exists(spath))
Directory.CreateDirectory(spath);
Process.Start(spath);
var result = MessageBox.Show(
"URL is already being tracked!\nOpen corresponding folder?",
"Error", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);

if (result == DialogResult.Yes)
{
var path = newTracker.SaveTo;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
Process.Start(path);
}
}
}
else
{
MessageBox.Show($"Entered text '{url}' is not a supported site or board/thread!");
}
}
else

if (trackerWasAdded)
{
MessageBox.Show($"Entered text '{url}' is not a supported site or board/thread!");
scanTimer.Enabled = true;
StartScanThread(this, new EventArgs());
}
}

/// <returns>Was <paramref name="tracker"/> added to a tracker list.</returns>
private bool AddNewTracker(Tracker tracker)
{
if (tracker == null)
Expand Down Expand Up @@ -235,7 +250,10 @@ internal void ClearTrackers(Type type)
}
}

private void Scan(object sender, EventArgs e)
/// <summary>
/// Run the scan thread if it isn't already running.
/// </summary>
private void StartScanThread(object sender, EventArgs e)
{
if (scanThread == null || !scanThread.IsAlive)
{
Expand All @@ -249,67 +267,71 @@ private void Scan(object sender, EventArgs e)
}
}

/// <summary>
/// Thread entry-point.
/// </summary>
private void ScanRoutine()
{
lock (ThreadLock)
{
// Remove 404'd threads
for (int i = 0; i < Model.Threads.Count; i++)
{
if (Model.Threads[i].Gone)
{
RemoveThread(Model.Threads[i]);
i--;
}
}
var removedThreads = Model.Threads.RemoveAll(t => t.Gone);
removedThreads.ForEach(t => RemoveThread(t));
}

// Make a copy of the current boards and scrape them for new threads.
var boards = Model.Boards.ToList();
var boards = Model.Boards.ToArray();

for (int i = 0; i < boards.Count; i++)
foreach (var board in boards)
{
if (boards[i].Scraping)
if (board.Scraping)
{
// OrderBy because we need the threads to be in ascending order by ID for LargestAddedThreadNo to be useful.
string[] boardThreadUrls = boards[i].GetThreadLinks().OrderBy(t => t).ToArray();
int largestNo = 0;
var threadUrls = board.GetThreadLinks();
var greatestThreadIdLock = new object();
var greatestThreadId = 0;

Parallel.ForEach(boardThreadUrls, (url) =>
Parallel.ForEach(threadUrls, (threadUrl) =>
{
if (boards[i].Scraping)
if (board.Scraping)
{
int? id = GetThreadId(boards[i], url);
var id = GetThreadId(board, threadUrl);

if (id.HasValue && id.Value > boards[i].LargestAddedThreadNo)
if (id != null && id > board.LargestAddedThreadNo)
{
Thread newThread = (Thread)Utils.CreateNewTracker(url);
var newThread = (Thread)Utils.CreateNewTracker(threadUrl);

if (newThread != null && IsUnique(newThread, Model.Threads))
{
bool urlWasAdded = AddNewTracker(newThread);
var urlWasAdded = AddNewTracker(newThread);

if (urlWasAdded)
{
if (id.Value > largestNo) //Not exactly safe in multithreaded but should work fine.
largestNo = id.Value;
lock (greatestThreadIdLock)
{
if (id > greatestThreadId)
{
greatestThreadId = id.Value;
}
}
}
}
}
}
});

boards[i].LargestAddedThreadNo = largestNo;
board.LargestAddedThreadNo = greatestThreadId;
}
}

// Make a copy of the current threads and download them.
var threads = Model.Threads.ToList();
var threads = Model.Threads.ToArray();

for (int i = 0; i < threads.Count; i++)
for (int i = 0; i < threads.Length; i++)
{
if (threads[i].Scraping)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(threads[i].Download));
}
}
}

Expand All @@ -319,8 +341,8 @@ private void ScanRoutine()
{
var idCodeMatch = board.SiteName switch
{
Thread_4Chan.ID_CODE_REGEX => Regex.Match(url, Thread_4Chan.ID_CODE_REGEX),
Board_8Kun.SITE_NAME_8KUN => Regex.Match(url, Thread_8Kun.ID_CODE_REGEX),
Board_4Chan.SITE_NAME => Regex.Match(url, Thread_4Chan.ID_CODE_REGEX),
Board_8Kun.SITE_NAME => Regex.Match(url, Thread_8Kun.ID_CODE_REGEX),
_ => null,
};

Expand All @@ -342,12 +364,13 @@ private void ScanRoutine()
/// </summary>
private bool IsUnique(Tracker tracker, IEnumerable<Tracker> list)
{
if (tracker.Type == Type.Thread)
if (tracker is Thread thread)
{
return !list.OfType<Thread>().Any(
t => t.SiteName == tracker.SiteName &&
t.BoardCode == tracker.BoardCode &&
t.ID == ((Thread)tracker).ID);
return !list.OfType<Thread>().Any(t =>
t.SiteName == thread.SiteName &&
t.BoardCode == thread.BoardCode &&
t.ID == thread.ID
);
}
else // Board
{
Expand Down Expand Up @@ -475,8 +498,8 @@ public bool Closing()
scanTimer.Dispose();

if (Settings.Default.SaveListsOnClose)
{
DataController.SaveAll(Model.Threads.ToList(), Model.Boards);
{
DataController.SaveAll(Model.Threads.ToArray(), Model.Boards.ToArray());
}

return false;
Expand Down
Loading

0 comments on commit a142b03

Please sign in to comment.