forked from babluboy/bookworm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backgroundTasks.vala
163 lines (157 loc) · 8.1 KB
/
backgroundTasks.vala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* Copyright 2017 Siddhartha Das ([email protected])
*
* This file is part of Bookworm and performs background
* related tasks like discovering books, cleaning cached data, etc.
* This code does not require a GUI/Display
* This code should be called from a sheduled job as "bookworm --discover"
*
* Bookworm is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Bookworm is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with Bookworm. If not, see http://www.gnu.org/licenses/.
*/
using Gee;
public class BookwormApp.BackgroundTasks {
public static ArrayList<string> listOfBooks;
public static BookwormApp.Settings settings;
public static void performTasks () {
initialization ();
//Add any new books from the watched folders
discoverBooks ();
//refresh list of books in DB due to new books being added
listOfBooks = BookwormApp.DB.getBookIDListFromDB ();
//Remove cahched data and cover thumbs which are no longer used
cleanBookCacheContent ();
cleanBookCoverImages ();
}
public static void initialization () {
settings = BookwormApp.Settings.get_instance ();
//check if the database exists otherwise create database and required tables
BookwormApp.DB.initializeBookWormDB (BookwormApp.Bookworm.bookworm_config_path);
listOfBooks = BookwormApp.DB.getBookIDListFromDB ();
}
public static void discoverBooks () {
print ("\nStarted process for discovery of books....");
ArrayList<string> scanDirList = new ArrayList<string> ();
//find the folders to scan from the settings
if (settings != null && settings.list_of_scan_dirs != null && settings.list_of_scan_dirs.length > 1) {
debug (settings.list_of_scan_dirs);
string[] scanDirArray = settings.list_of_scan_dirs.split ("~~");
foreach (string token in scanDirArray) {
scanDirList.add (token);
}
if (scanDirList.size > 0) {
//create the find command
StringBuilder findCmd = new StringBuilder ("find ");
foreach (string scanDir in scanDirList) {
if (scanDir != null && scanDir.length > 1) {
findCmd.append ("\"").append (scanDir).append ("\"").append (" ");
}
}
findCmd.append ("! -readable -prune -o -type f \\( -iname \\*.mobi -o -iname \\*.pdf -o -iname \\*.epub -o -iname \\*.cbr -o -iname \\*.cbz \\) -print");
string findCmdOutput = BookwormApp.Utils.execute_sync_command (findCmd.str);
if (findCmdOutput.contains ("\n")) {
string[] findCmdOutputResults = findCmdOutput.strip ().split ("\n",-1);
foreach (string findResult in findCmdOutputResults) {
bool noMatchFound = true;
foreach (string book in listOfBooks) {
if (book.contains (findResult)) {
noMatchFound = false;
break;
}
}
if (noMatchFound) {
print ("\nAttempting to add book located at:" + findResult);
BookwormApp.Book aBook = new BookwormApp.Book ();
aBook.setBookLocation (findResult);
File eBookFile = File.new_for_path (findResult);
if (eBookFile.query_exists () && eBookFile.query_file_type (0) != FileType.DIRECTORY) {
int bookID = BookwormApp.DB.addBookToDataBase (aBook);
aBook.setBookId (bookID);
aBook.setBookLastModificationDate ((new DateTime.now_utc ().to_unix ()).to_string ());
aBook.setWasBookOpened (true);
//parse eBook to populate cache and book meta data
aBook = BookwormApp.Bookworm.genericParser (aBook);
if (!aBook.getIsBookParsed ()) {
BookwormApp.DB.removeBookFromDB (aBook);
} else {
BookwormApp.DB.updateBookToDataBase (aBook);
print ("\nSuccessfully added book located at:" + findResult);
}
}
}
}
}
print ("\nCompleted process for discovery of books....\n");
}
}
}
public static void cleanBookCacheContent () {
print ("\nStarting to delete un-necessary cache data...");
//list the folders in the cache
string cacheFolders = BookwormApp.Utils.execute_sync_command ("ls -1 " + BookwormApp.Bookworm.bookworm_config_path + "/books/");
cacheFolders = cacheFolders.replace ("\r", "^^^").replace ("\n", "^^^");
string[] cacheFolderList = cacheFolders.split ("^^^");
//loop through each folder name
bool folderMatched = false;
foreach (string cacheFolder in cacheFolderList) {
folderMatched = false;
cacheFolder = cacheFolder.strip ();
if (cacheFolder == null || cacheFolder.length < 1) {
folderMatched = true;
}
foreach (string bookData in listOfBooks) {
if (cacheFolder != null && cacheFolder.length > 0) {
//check if the folder is part of a book in the library
if ((bookData.split ("::")[1]).index_of (cacheFolder) != -1) {
folderMatched = true;
break;
}
}
}
if (!folderMatched) {
//delete the folder and content if it is not a part of any book in the library
BookwormApp.Utils.execute_sync_command ("rm -Rf \"" + BookwormApp.Bookworm.bookworm_config_path + "/books/" + cacheFolder + "\"");
print ("\nCache Folder deleted:" + cacheFolder);
}
}
}
public static void cleanBookCoverImages () {
print ("\nStarting to delete un-necessary cover image data...");
//list the cover images in the cache
string cacheImages = BookwormApp.Utils.execute_sync_command ("ls -1 " + BookwormApp.Bookworm.bookworm_config_path + "/covers/");
cacheImages = cacheImages.replace ("\r", "^^^").replace ("\n", "^^^");
string[] cacheImageList = cacheImages.split ("^^^");
//loop through each cover image in cache
bool imageMatched = false;
foreach (string cacheImage in cacheImageList) {
imageMatched = false;
cacheImage = cacheImage.strip ();
if (cacheImage == null || cacheImage.length < 1) {
imageMatched = true;
}
foreach (string bookData in listOfBooks) {
if (cacheImage != null && cacheImage.length > 0) {
//check if the folder is part of a book in the library
if (cacheImage.index_of ((bookData.split ("::")[0])) != -1) {
imageMatched = true;
break;
}
}
}
if (!imageMatched) {
//delete the folder and content if it is not a part of any book in the library
BookwormApp.Utils.execute_sync_command ("rm -f \"" + BookwormApp.Bookworm.bookworm_config_path + "/covers/" + cacheImage + "\"");
print ("\nCache Image deleted:" + cacheImage);
}
}
}
}