Skip to content

Commit

Permalink
Scan non-fiction
Browse files Browse the repository at this point in the history
  • Loading branch information
libgenapps committed May 21, 2018
1 parent 90dbf66 commit 2e4258b
Show file tree
Hide file tree
Showing 26 changed files with 454 additions and 9 deletions.
4 changes: 2 additions & 2 deletions LibgenDesktop.Setup/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
{
internal static class Constants
{
public const string CURRENT_VERSION = "1.1.1";
public const string TITLE_VERSION = "1.1.1";
public const string CURRENT_VERSION = "1.1.2";
public const string TITLE_VERSION = "1.1.2";
public const string PRODUCT_TITLE_FORMAT = "Libgen Desktop " + TITLE_VERSION + " ({0}-bit)";
public const string SHORTCUT_TITLE_FORMAT = "Libgen Desktop ({0}-bit)";
public const string PRODUCT_COMPANY = "Libgen Apps";
Expand Down
10 changes: 7 additions & 3 deletions LibgenDesktop/Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace LibgenDesktop.Common
{
internal static class Constants
{
public const string CURRENT_VERSION = "1.1.1";
public const string CURRENT_GITHUB_RELEASE_NAME = "1.1.1";
public static readonly DateTime CURRENT_GITHUB_RELEASE_DATE = new DateTime(2018, 5, 2);
public const string CURRENT_VERSION = "1.1.2";
public const string CURRENT_GITHUB_RELEASE_NAME = "1.1.2";
public static readonly DateTime CURRENT_GITHUB_RELEASE_DATE = new DateTime(2018, 5, 21);
public const string CURRENT_DATABASE_VERSION = "1.0";

public const string APP_SETTINGS_FILE_NAME = "libgen.config";
Expand Down Expand Up @@ -56,6 +56,10 @@ internal static class Constants
public const int IMPORT_WINDOW_MIN_WIDTH = 530;
public const int IMPORT_WINDOW_MIN_HEIGHT = 400;
public const int CREATE_DATABASE_WINDOW_WIDTH = 500;
public const int LIBRARY_WINDOW_DEFAULT_WIDTH = 760;
public const int LIBRARY_WINDOW_DEFAULT_HEIGHT = 550;
public const int LIBRARY_WINDOW_MIN_WIDTH = 760;
public const int LIBRARY_WINDOW_MIN_HEIGHT = 550;
public const int SETTINGS_WINDOW_DEFAULT_WIDTH = 760;
public const int SETTINGS_WINDOW_DEFAULT_HEIGHT = 550;
public const int SETTINGS_WINDOW_MIN_WIDTH = 760;
Expand Down
2 changes: 2 additions & 0 deletions LibgenDesktop/Infrastructure/RegisteredWindows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal enum WindowKey
SETTINGS_WINDOW,
SYNCHRONIZATION_WINDOW,
APPLICATION_UPDATE_WINDOW,
LIBRARY_WINDOW,
DATABASE_WINDOW,
ABOUT_WINDOW
}
Expand Down Expand Up @@ -49,6 +50,7 @@ static RegisteredWindows()
RegisterWindow(WindowKey.CREATE_DATABASE_WINDOW, typeof(CreateDatabaseWindow), typeof(CreateDatabaseWindowViewModel));
RegisterWindow(WindowKey.SETTINGS_WINDOW, typeof(SettingsWindow), typeof(SettingsWindowViewModel));
RegisterWindow(WindowKey.SYNCHRONIZATION_WINDOW, typeof(SynchronizationWindow), typeof(SynchronizationWindowViewModel));
RegisterWindow(WindowKey.LIBRARY_WINDOW, typeof(LibraryWindow), typeof(LibraryWindowViewModel));
RegisterWindow(WindowKey.APPLICATION_UPDATE_WINDOW, typeof(ApplicationUpdateWindow), typeof(ApplicationUpdateWindowViewModel));
RegisterWindow(WindowKey.DATABASE_WINDOW, typeof(DatabaseWindow), typeof(DatabaseWindowViewModel));
RegisterWindow(WindowKey.ABOUT_WINDOW, typeof(AboutWindow), typeof(AboutWindowViewModel));
Expand Down
16 changes: 16 additions & 0 deletions LibgenDesktop/LibgenDesktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
<Compile Include="Models\Localization\Localizators\ApplicationUpdateLocalizator.cs" />
<Compile Include="Models\Localization\Localizators\CommonDetailsTabLocalizator.cs" />
<Compile Include="Models\Localization\Localizators\AboutWindowLocalizator.cs" />
<Compile Include="Models\Localization\Localizators\LibraryWindowLocalizator.cs" />
<Compile Include="Models\Localization\Localizators\DatabaseWindowLocalizator.cs" />
<Compile Include="Models\Localization\Localizators\CreateDatabaseWindowLocalizator.cs" />
<Compile Include="Models\Localization\Localizators\DetailsTabLocalizator.cs" />
Expand Down Expand Up @@ -201,12 +202,15 @@
<Compile Include="Models\Localization\Localizators\SynchronizationLocalizator.cs" />
<Compile Include="Models\Localization\Translation.cs" />
<Compile Include="Models\ProgressArgs\DownloadFileProgress.cs" />
<Compile Include="Models\ProgressArgs\GenericProgress.cs" />
<Compile Include="Models\ProgressArgs\ImportCreateIndexProgress.cs" />
<Compile Include="Models\ProgressArgs\ImportLoadLibgenIdsProgress.cs" />
<Compile Include="Models\ProgressArgs\ImportObjectsProgress.cs" />
<Compile Include="Models\ProgressArgs\ImportSearchTableDefinitionProgress.cs" />
<Compile Include="Models\ProgressArgs\ImportTableDefinitionFoundProgress.cs" />
<Compile Include="Models\ProgressArgs\ExportProgress.cs" />
<Compile Include="Models\ProgressArgs\ScanCompleteProgress.cs" />
<Compile Include="Models\ProgressArgs\ScanProgress.cs" />
<Compile Include="Models\ProgressArgs\SearchProgress.cs" />
<Compile Include="Models\ProgressArgs\SynchronizationProgress.cs" />
<Compile Include="Models\Settings\Mirrors.cs" />
Expand Down Expand Up @@ -239,6 +243,7 @@
<Compile Include="ViewModels\Windows\ApplicationUpdateWindowViewModel.cs" />
<Compile Include="ViewModels\Tabs\DetailsTabViewModel.cs" />
<Compile Include="ViewModels\Windows\AboutWindowViewModel.cs" />
<Compile Include="ViewModels\Windows\LibraryWindowViewModel.cs" />
<Compile Include="ViewModels\Windows\DatabaseWindowViewModel.cs" />
<Compile Include="ViewModels\Windows\DetailsWindowViewModel.cs" />
<Compile Include="ViewModels\EventArguments\OpenFictionDetailsEventArgs.cs" />
Expand Down Expand Up @@ -295,6 +300,9 @@
<Compile Include="Views\Windows\AboutWindow.xaml.cs">
<DependentUpon>AboutWindow.xaml</DependentUpon>
</Compile>
<Compile Include="Views\Windows\LibraryWindow.xaml.cs">
<DependentUpon>LibraryWindow.xaml</DependentUpon>
</Compile>
<Compile Include="Views\Windows\DatabaseWindow.xaml.cs">
<DependentUpon>DatabaseWindow.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -376,6 +384,10 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Views\Styles\LibraryWindowStyles.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Views\Styles\DatabaseWindowStyles.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down Expand Up @@ -408,6 +420,10 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Views\Windows\LibraryWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Views\Windows\DatabaseWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down
26 changes: 26 additions & 0 deletions LibgenDesktop/Models/Database/LocalDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ public void CreateNonFictionTables()
ExecuteCommands(SqlScripts.CREATE_NON_FICTION_FTS_TABLE);
}

public void CreateNonFictionMd5HashIndex()
{
ExecuteCommands(SqlScripts.CREATE_NON_FICTION_MD5HASH_INDEX);
}

public void CreateNonFictionLastModifiedDateTimeIndex()
{
ExecuteCommands(SqlScripts.CREATE_NON_FICTION_LASTMODIFIEDDATETIME_INDEX);
Expand Down Expand Up @@ -174,6 +179,27 @@ public NonFictionBook GetNonFictionBookById(int id)
}
}

public NonFictionBook GetNonFictionBookByMd5Hash(string md5Hash)
{
using (SQLiteCommand command = connection.CreateCommand())
{
command.CommandText = SqlScripts.GET_NON_FICTION_BY_MD5HASH;
command.Parameters.AddWithValue("@Md5Hash", md5Hash);
using (SQLiteDataReader dataReader = command.ExecuteReader())
{
if (dataReader.Read())
{
NonFictionBook book = ReadNonFictionBook(dataReader);
return book;
}
else
{
return null;
}
}
}
}

public int? GetNonFictionBookIdByLibgenId(int libgenId)
{
return GetIdByLibgenId(SqlScripts.GET_NON_FICTION_ID_BY_LIBGENID, libgenId);
Expand Down
9 changes: 9 additions & 0 deletions LibgenDesktop/Models/Database/SqlScripts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ internal static class SqlScripts

public const string GET_NON_FICTION_BY_ID = "SELECT * FROM non_fiction WHERE Id = @Id";

public const string GET_NON_FICTION_BY_MD5HASH = "SELECT * FROM non_fiction WHERE Md5Hash = @Md5Hash COLLATE NOCASE";

public const string GET_NON_FICTION_ID_BY_LIBGENID = "SELECT Id FROM non_fiction WHERE LibgenId=@LibgenId LIMIT 1";

public const string GET_LAST_MODIFIED_NON_FICTION =
Expand Down Expand Up @@ -162,6 +164,9 @@ internal static class SqlScripts

public const string NON_FICTION_INDEX_PREFIX = "IX_non_fiction_";

public const string CREATE_NON_FICTION_MD5HASH_INDEX = "CREATE UNIQUE INDEX " + NON_FICTION_INDEX_PREFIX +
"Md5Hash ON non_fiction (Md5Hash COLLATE NOCASE)";

public const string CREATE_NON_FICTION_LASTMODIFIEDDATETIME_INDEX = "CREATE INDEX " + NON_FICTION_INDEX_PREFIX +
"LastModifiedDateTime ON non_fiction (LastModifiedDateTime DESC)";

Expand Down Expand Up @@ -329,6 +334,8 @@ internal static class SqlScripts

public const string FICTION_INDEX_PREFIX = "IX_fiction_";

public const string CREATE_FICTION_MD5HASH_INDEX = "CREATE UNIQUE INDEX " + FICTION_INDEX_PREFIX + "Md5Hash ON fiction (Md5Hash COLLATE NOCASE)";

public const string CREATE_FICTION_LASTMODIFIEDDATETIME_INDEX = "CREATE INDEX " + FICTION_INDEX_PREFIX +
"LastModifiedDateTime ON fiction (LastModifiedDateTime DESC)";

Expand Down Expand Up @@ -399,6 +406,8 @@ internal static class SqlScripts

public const string SCIMAG_INDEX_PREFIX = "IX_scimag_";

public const string CREATE_SCIMAG_MD5HASH_INDEX = "CREATE UNIQUE INDEX " + SCIMAG_INDEX_PREFIX + "Md5Hash ON scimag (Md5Hash COLLATE NOCASE)";

public const string CREATE_SCIMAG_ADDEDDATETIME_INDEX = "CREATE INDEX " + SCIMAG_INDEX_PREFIX + "AddedDateTime ON scimag (AddedDateTime DESC)";
}
}
3 changes: 3 additions & 0 deletions LibgenDesktop/Models/Localization/Language.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ internal class Language
private SynchronizationLocalizator synchronization;
private DownloadManagerLocalizator downloadManager;
private ApplicationUpdateLocalizator applicationUpdate;
private LibraryWindowLocalizator library;
private DatabaseWindowLocalizator database;
private SettingsWindowLocalizator settings;
private AboutWindowLocalizator about;
Expand Down Expand Up @@ -127,6 +128,8 @@ public Language(List<Translation> prioritizedTranslationList)
public ApplicationUpdateLocalizator ApplicationUpdate =>
applicationUpdate ?? (applicationUpdate = new ApplicationUpdateLocalizator(translations, Formatter));

public LibraryWindowLocalizator Library => library ?? (library = new LibraryWindowLocalizator(translations, Formatter));

public DatabaseWindowLocalizator Database => database ?? (database = new DatabaseWindowLocalizator(translations, Formatter));

public SettingsWindowLocalizator Settings => settings ?? (settings = new SettingsWindowLocalizator(translations, Formatter));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;

namespace LibgenDesktop.Models.Localization.Localizators
{
internal class LibraryWindowLocalizator : Localizator
{
public LibraryWindowLocalizator(List<Translation> prioritizedTranslationList, LanguageFormatter formatter)
: base(prioritizedTranslationList, formatter)
{
WindowTitle = Format(translation => translation?.WindowTitle);
Scan = Format(translation => translation?.Scan);
BrowseDirectoryDialogTitle = Format(translation => translation?.BrowseDirectoryDialogTitle);
CreatingIndexes = Format(translation => translation?.CreatingIndexes);
NotFound = Format(translation => translation?.NotFound);
}

public string WindowTitle { get; }
public string Scan { get; }
public string BrowseDirectoryDialogTitle { get; }
public string CreatingIndexes { get; }
public string NotFound { get; }

public string GetScanStartedString(string directory) => Format(translation => translation?.ScanStarted, new { directory });
public string GetScanCompleteString(int found, int notFound) => Format(translation => translation?.ScanComplete,
new { found = Formatter.ToFormattedString(found), notFound = Formatter.ToFormattedString(notFound) });

private string Format(Func<Translation.LibraryTranslation, string> field, object templateArguments = null)
{
return Format(translation => field(translation?.Library), templateArguments);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace LibgenDesktop.Models.Localization.Localizators
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public MainWindowLocalizator(List<Translation> prioritizedTranslationList, Langu
ToolbarUpdate = Format(translation => translation?.Update);
ToolbarImport = Format(translation => translation?.Import);
ToolbarSynchronize = Format(translation => translation?.Synchronize);
ToolbarLibrary = Format(translation => translation?.Library);
ToolbarDatabase = Format(translation => translation?.Database);
ToolbarSettings = Format(translation => translation?.Settings);
ToolbarAbout = Format(translation => translation?.About);
Expand All @@ -27,6 +28,7 @@ public MainWindowLocalizator(List<Translation> prioritizedTranslationList, Langu
public string ToolbarUpdate { get; }
public string ToolbarImport { get; }
public string ToolbarSynchronize { get; }
public string ToolbarLibrary { get; }
public string ToolbarDatabase { get; }
public string ToolbarSettings { get; }
public string ToolbarAbout { get; }
Expand Down
13 changes: 13 additions & 0 deletions LibgenDesktop/Models/Localization/Translation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ internal class MainMenuTranslation
public string Update { get; set; }
public string Import { get; set; }
public string Synchronize { get; set; }
public string Library { get; set; }
public string Database { get; set; }
public string Settings { get; set; }
public string About { get; set; }
Expand Down Expand Up @@ -543,6 +544,17 @@ internal class SynchronizationTranslation
public SynchronizationLogMessagesTranslation LogMessages { get; set; }
}

internal class LibraryTranslation
{
public string WindowTitle { get; set; }
public string Scan { get; set; }
public string BrowseDirectoryDialogTitle { get; set; }
public string ScanStarted { get; set; }
public string CreatingIndexes { get; set; }
public string NotFound { get; set; }
public string ScanComplete { get; set; }
}

internal class DatabaseTranslation
{
public string WindowTitle { get; set; }
Expand Down Expand Up @@ -794,6 +806,7 @@ internal class ErrorWindowTranslation
public SynchronizationTranslation Synchronization { get; set; }
public DownloadManagerTranslation DownloadManager { get; set; }
public ApplicationUpdateTranslation ApplicationUpdate { get; set; }
public LibraryTranslation Library { get; set; }
public DatabaseTranslation Database { get; set; }
public SettingsTranslation Settings { get; set; }
public AboutTranslation About { get; set; }
Expand Down
66 changes: 66 additions & 0 deletions LibgenDesktop/Models/MainModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using LibgenDesktop.Common;
Expand Down Expand Up @@ -482,6 +483,28 @@ public Task<DownloadFileResult> DownloadFileAsync(string fileUrl, string destina
});
}

public Task ScanAsync(string scanDirectory, IProgress<object> progressHandler)
{
return Task.Run(() =>
{
int found = 0;
int notFound = 0;
if (NonFictionBookCount > 0)
{
Logger.Debug("Retrieving the list of non-fiction table indexes.");
List<string> nonFictionIndexes = localDatabase.GetNonFictionIndexList();
if (!nonFictionIndexes.Contains(SqlScripts.NON_FICTION_INDEX_PREFIX + "Md5Hash"))
{
Logger.Debug("Creating an index on Md5Hash column.");
progressHandler.Report(new GenericProgress(GenericProgress.Event.SCAN_CREATING_INDEXES));
localDatabase.CreateNonFictionMd5HashIndex();
}
ScanDirectory(scanDirectory.ToLower(), scanDirectory, progressHandler, ref found, ref notFound);
}
progressHandler.Report(new ScanCompleteProgress(found, notFound));
});
}

public Task<DatabaseStats> GetDatabaseStatsAsync()
{
return Task.Run(() =>
Expand Down Expand Up @@ -886,6 +909,49 @@ private TableType DetectImportTableType(SqlDumpReader.ParsedTableDefinition pars
return TableType.UNKNOWN;
}

private void ScanDirectory(string rootScanDirectory, string scanDirectory, IProgress<object> progressHandler, ref int found, ref int notFound)
{
foreach (string filePath in Directory.EnumerateFiles(scanDirectory))
{
string md5Hash;
try
{
using (MD5 md5 = MD5.Create())
using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
byte[] md5HashArray = md5.ComputeHash(fileStream);
md5Hash = BitConverter.ToString(md5HashArray).Replace("-", String.Empty).ToLowerInvariant();
}
}
catch (Exception exception)
{
Logger.Debug($"Couldn't calculate MD5 hash for the file: {filePath}");
Logger.Exception(exception);
continue;
}
string relativeFilePath = filePath;
if (relativeFilePath.ToLower().StartsWith(rootScanDirectory))
{
relativeFilePath = relativeFilePath.Substring(rootScanDirectory.Length + 1);
}
NonFictionBook book = localDatabase.GetNonFictionBookByMd5Hash(md5Hash);
if (book != null)
{
progressHandler.Report(new ScanProgress(relativeFilePath, book.Authors, book.Title));
found++;
}
else
{
progressHandler.Report(new ScanProgress(relativeFilePath));
notFound++;
}
}
foreach (string directoryPath in Directory.EnumerateDirectories(scanDirectory))
{
ScanDirectory(rootScanDirectory, directoryPath, progressHandler, ref found, ref notFound);
}
}

private void CheckAndCreateNonFictionIndexes(IProgress<object> progressHandler, CancellationToken cancellationToken)
{
Logger.Debug("Retrieving the list of non-fiction table indexes.");
Expand Down
Loading

0 comments on commit 2e4258b

Please sign in to comment.