From 1c48356c9671023f35e03f08ad7c03982e9f19b5 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 21 Nov 2008 16:46:04 -0800 Subject: [PATCH] IGN:Add option to automatically delete old news --- src/calibre/gui2/__init__.py | 2 +- src/calibre/gui2/dialogs/scheduler.py | 17 ++++++++++++++++- src/calibre/gui2/dialogs/scheduler.ui | 22 +++++++++++++++++++--- src/calibre/gui2/library.py | 13 +++++++++++++ src/calibre/library/database2.py | 12 ++++++++++++ 5 files changed, 61 insertions(+), 5 deletions(-) diff --git a/src/calibre/gui2/__init__.py b/src/calibre/gui2/__init__.py index 8f1a236134..6f398ec9d6 100644 --- a/src/calibre/gui2/__init__.py +++ b/src/calibre/gui2/__init__.py @@ -52,7 +52,7 @@ def _config(): c.add_opt('column_map', default=ALL_COLUMNS, help=_('Columns to be displayed in the book list')) c.add_opt('autolaunch_server', default=False, help=_('Automatically launch content server on application startup')) - + c.add_opt('oldest_news', default=60, help=_('Oldest news kept in database')) return ConfigProxy(c) config = _config() diff --git a/src/calibre/gui2/dialogs/scheduler.py b/src/calibre/gui2/dialogs/scheduler.py index cdbf0a6dcd..64966b0961 100644 --- a/src/calibre/gui2/dialogs/scheduler.py +++ b/src/calibre/gui2/dialogs/scheduler.py @@ -18,7 +18,7 @@ from calibre.gui2.dialogs.scheduler_ui import Ui_Dialog from calibre.web.feeds.recipes import recipes, recipe_modules, compile_recipe from calibre.utils.search_query_parser import SearchQueryParser from calibre.utils.pyparsing import ParseException -from calibre.gui2 import NONE, error_dialog +from calibre.gui2 import NONE, error_dialog, config as gconf from calibre.utils.config import DynamicConfig from calibre.gui2.dialogs.user_profiles import UserProfiles @@ -227,6 +227,7 @@ class SchedulerDialog(QDialog, Ui_Dialog): self.connect(self._model, SIGNAL('modelReset()'), lambda : self.detail_box.setVisible(False)) self.connect(self.download, SIGNAL('clicked()'), self.download_now) self.search.setFocus(Qt.OtherFocusReason) + self.old_news.setValue(gconf['oldest_news']) def download_now(self): recipe = self._model.data(self.recipes.currentIndex(), Qt.UserRole) @@ -308,6 +309,11 @@ class Scheduler(QObject): self.dirtied = False self.connect(self.timer, SIGNAL('timeout()'), self.check) self.timer.start(int(self.INTERVAL * 60000)) + self.oldest_timer = QTimer() + self.connect(self.oldest_timer, SIGNAL('timeout()'), self.oldest_check) + self.oldest = gconf['oldest_news'] + self.oldest_timer.start(int(60 * 60000)) + self.oldest_check() self.news_menu = QMenu() self.news_icon = QIcon(':/images/news.svg') @@ -318,6 +324,13 @@ class Scheduler(QObject): self.connect(self.cac, SIGNAL('triggered(bool)'), self.customize_feeds) self.news_menu.addAction(self.cac) + def oldest_check(self): + if self.oldest > 0: + delta = timedelta(days=self.oldest) + ids = self.main.library_view.model().db.tags_older_than(_('News'), delta) + if ids: + self.main.library_view.model().delete_books_by_id(ids) + def customize_feeds(self, *args): main = self.main d = UserProfiles(main, main.library_view.model().db.get_feeds()) @@ -412,7 +425,9 @@ class Scheduler(QObject): self.connect(d, SIGNAL('new_schedule(PyQt_PyObject)'), self.refresh_schedule) self.connect(d, SIGNAL('download_now(PyQt_PyObject)'), self.download) d.exec_() + gconf['oldest_news'] = d.old_news.value() self.recipes = load_recipes() + self.oldest = d.old_news.value() finally: self.lock.unlock() diff --git a/src/calibre/gui2/dialogs/scheduler.ui b/src/calibre/gui2/dialogs/scheduler.ui index 5986e2b75a..e4084cfee7 100644 --- a/src/calibre/gui2/dialogs/scheduler.ui +++ b/src/calibre/gui2/dialogs/scheduler.ui @@ -17,7 +17,7 @@ :/images/scheduler.svg:/images/scheduler.svg - + Recipes @@ -164,7 +164,7 @@ - + @@ -262,7 +262,7 @@ - + Qt::Horizontal @@ -272,6 +272,22 @@ + + + + Delete downloaded news older than the specified number of days. Set to zero to disable. + + + days + + + Delete downloaded news older than + + + 1000 + + + diff --git a/src/calibre/gui2/library.py b/src/calibre/gui2/library.py index 96a03c1a57..7b2ac8e20a 100644 --- a/src/calibre/gui2/library.py +++ b/src/calibre/gui2/library.py @@ -188,6 +188,19 @@ class BooksModel(QAbstractTableModel): self.endRemoveRows() self.clear_caches() self.reset() + + def delete_books_by_id(self, ids): + for id in ids: + try: + row = self.db.row(id) + except: + row = -1 + if row > -1: + self.beginRemoveRows(QModelIndex(), row, row) + self.db.delete_book(id) + if row > -1: + self.endRemoveRows() + self.clear_caches() def books_added(self, num): if num > 0: diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index 0b9a8729ab..8c74620a3d 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -749,6 +749,18 @@ class LibraryDatabase2(LibraryDatabase): return categories + def tags_older_than(self, tag, delta): + tag = tag.lower().strip() + now = datetime.now() + for r in self.data._data: + if r is not None: + if (now - r[FIELD_MAP['timestamp']]) > delta: + tags = r[FIELD_MAP['tags']] + if tags and tag in tags.lower(): + yield r[FIELD_MAP['id']] + + + def set(self, row, column, val): ''' Convenience method for setting the title, authors, publisher or rating