Refactorings...

This commit is contained in:
Martin Rotter 2013-12-23 14:43:47 +01:00
parent a49df4f2cf
commit bba6788dfc
14 changed files with 98 additions and 45 deletions

View File

@ -29,7 +29,7 @@
#define NO_PARENT_CATEGORY -1 #define NO_PARENT_CATEGORY -1
#define TRAY_ICON_BUBBLE_TIMEOUT 15000 #define TRAY_ICON_BUBBLE_TIMEOUT 15000
#define KEY_MESSAGES_VIEW "messages_view_column_" #define KEY_MESSAGES_VIEW "messages_view_column_"
#define CLOSE_LOCK_TIMEOUT 2000 #define CLOSE_LOCK_TIMEOUT 3000
#define APP_DB_INIT_FILE "db_init.sql" #define APP_DB_INIT_FILE "db_init.sql"
#define APP_DB_INIT_SPLIT "-- !\n" #define APP_DB_INIT_SPLIT "-- !\n"

View File

@ -1,10 +1,12 @@
#include "core/feeddownloader.h" #include "core/feeddownloader.h"
#include "core/feedsmodelfeed.h" #include "core/feedsmodelfeed.h"
#include "core/systemfactory.h"
#include "core/silentnetworkaccessmanager.h" #include "core/silentnetworkaccessmanager.h"
#include <QThread> #include <QThread>
#include <QDebug> #include <QDebug>
#include <QReadWriteLock>
#include <QApplication> #include <QApplication>
@ -26,14 +28,20 @@ SilentNetworkAccessManager *FeedDownloader::globalNetworkManager() {
} }
void FeedDownloader::updateFeeds(const QList<FeedsModelFeed *> &feeds) { void FeedDownloader::updateFeeds(const QList<FeedsModelFeed *> &feeds) {
qDebug().nospace() << "Creating main application form in thread: \'" << qDebug().nospace() << "Performing feed updates in thread: \'" <<
QThread::currentThreadId() << "\'."; QThread::currentThreadId() << "\'.";
for (int i = 0, total = feeds.size(); i < total; i++) { for (int i = 0, total = feeds.size(); i < total; i++) {
feeds.at(i)->update(); feeds.at(i)->update();
qDebug("Made progress in feed updates: %d/%d.", i, total);
emit progress(feeds.at(i), i + 1, total); emit progress(feeds.at(i), i + 1, total);
} }
qDebug().nospace() << "Finished feed updates in thread: \'" <<
QThread::currentThreadId() << "\'.";
// Update of feeds has finished. // Update of feeds has finished.
// NOTE: This means that now "update lock" can be unlocked // NOTE: This means that now "update lock" can be unlocked
// and feeds can be added/edited/deleted and application // and feeds can be added/edited/deleted and application

View File

@ -37,6 +37,9 @@ void FeedsModelFeed::setCountOfUnreadMessages(int count) {
m_unreadCount = count; m_unreadCount = count;
} }
void FeedsModelFeed::update() {
}
FeedsModelFeed::Type FeedsModelFeed::type() const { FeedsModelFeed::Type FeedsModelFeed::type() const {
return m_type; return m_type;
} }

View File

@ -34,6 +34,12 @@ class FeedsModelFeed : public FeedsModelRootItem {
int countOfUnreadMessages() const; int countOfUnreadMessages() const;
void setCountOfUnreadMessages(int count); void setCountOfUnreadMessages(int count);
// Each feed can be "updated".
// NOTE: This method is used in the "update worker".
// For example, it can fetch new messages from a remote destination
// and store them in a local database and so on.
virtual void update();
// Other getters/setters. // Other getters/setters.
Type type() const; Type type() const;
void setType(const Type &type); void setType(const Type &type);

View File

@ -23,9 +23,6 @@ void FeedsModelRootItem::setParent(FeedsModelRootItem *parent_item) {
m_parentItem = parent_item; m_parentItem = parent_item;
} }
void FeedsModelRootItem::update() {
}
FeedsModelRootItem::Kind FeedsModelRootItem::kind() const { FeedsModelRootItem::Kind FeedsModelRootItem::kind() const {
return m_kind; return m_kind;
} }

View File

@ -34,12 +34,6 @@ class FeedsModelRootItem {
virtual int countOfUnreadMessages() const; virtual int countOfUnreadMessages() const;
virtual int countOfAllMessages() const; virtual int countOfAllMessages() const;
// Each item can be "updated".
// NOTE: This method is used in the "update worker".
// For example, it can fetch new messages from a remote destination
// and store them in a local database and so on.
virtual void update();
virtual Kind kind() const; virtual Kind kind() const;
// Each item has icon. // Each item has icon.

View File

@ -84,13 +84,6 @@ QVariant FeedsModelStandardCategory::data(int column, int role) const {
} }
} }
void FeedsModelStandardCategory::update() {
// Update all children.
foreach (FeedsModelRootItem *child, m_childItems) {
child->update();
}
}
FeedsModelStandardCategory *FeedsModelStandardCategory::loadFromRecord(const QSqlRecord &record) { FeedsModelStandardCategory *FeedsModelStandardCategory::loadFromRecord(const QSqlRecord &record) {
FeedsModelStandardCategory *category = new FeedsModelStandardCategory(NULL); FeedsModelStandardCategory *category = new FeedsModelStandardCategory(NULL);

View File

@ -21,9 +21,6 @@ class FeedsModelStandardCategory : public FeedsModelCategory {
// Returns the actual data representation of standard category. // Returns the actual data representation of standard category.
QVariant data(int column, int role) const; QVariant data(int column, int role) const;
// Performs update on all children of this category.
void update();
// Loads particular "standard category" from given sql record. // Loads particular "standard category" from given sql record.
static FeedsModelStandardCategory *loadFromRecord(const QSqlRecord &record); static FeedsModelStandardCategory *loadFromRecord(const QSqlRecord &record);
}; };

View File

@ -5,6 +5,8 @@
#include "gui/iconthemefactory.h" #include "gui/iconthemefactory.h"
#include <QVariant> #include <QVariant>
#include <QThread>
#include <unistd.h>
FeedsModelStandardFeed::FeedsModelStandardFeed(FeedsModelRootItem *parent_item) FeedsModelStandardFeed::FeedsModelStandardFeed(FeedsModelRootItem *parent_item)
@ -135,5 +137,5 @@ QVariant FeedsModelStandardFeed::data(int column, int role) const {
} }
void FeedsModelStandardFeed::update() { void FeedsModelStandardFeed::update() {
// TODO: Perform fetching of new messages. usleep(5500000);
} }

View File

@ -3,6 +3,8 @@
#include "core/settings.h" #include "core/settings.h"
#include "core/messagesproxymodel.h" #include "core/messagesproxymodel.h"
#include "core/feeddownloader.h" #include "core/feeddownloader.h"
#include "core/feedsmodelstandardfeed.h"
#include "core/systemfactory.h"
#include "gui/webbrowser.h" #include "gui/webbrowser.h"
#include "gui/formmain.h" #include "gui/formmain.h"
#include "gui/iconthemefactory.h" #include "gui/iconthemefactory.h"
@ -18,6 +20,8 @@
#include <QToolButton> #include <QToolButton>
#include <QMenu> #include <QMenu>
#include <QWidgetAction> #include <QWidgetAction>
#include <QThread>
#include <QReadWriteLock>
FeedMessageViewer::FeedMessageViewer(QWidget *parent) FeedMessageViewer::FeedMessageViewer(QWidget *parent)
@ -25,10 +29,15 @@ FeedMessageViewer::FeedMessageViewer(QWidget *parent)
m_toolBar(new QToolBar(tr("Toolbar for messages"), this)), m_toolBar(new QToolBar(tr("Toolbar for messages"), this)),
m_messagesView(new MessagesView(this)), m_messagesView(new MessagesView(this)),
m_feedsView(new FeedsView(this)), m_feedsView(new FeedsView(this)),
m_messagesBrowser(new WebBrowser(this)) { m_messagesBrowser(new WebBrowser(this)),
m_feedDownloaderThread(new QThread()),
m_feedDownloader(new FeedDownloader()) {
initialize(); initialize();
initializeViews(); initializeViews();
createConnections(); createConnections();
// Start the feed downloader thread.
m_feedDownloaderThread->start();
} }
FeedMessageViewer::~FeedMessageViewer() { FeedMessageViewer::~FeedMessageViewer() {
@ -75,6 +84,27 @@ void FeedMessageViewer::loadSize() {
default_msg_section_size).toInt()); default_msg_section_size).toInt());
} }
void FeedMessageViewer::quitDownloader() {
qDebug("Quitting feed downloader thread.");
m_feedDownloaderThread->quit();
m_feedDownloader->deleteLater();
}
void FeedMessageViewer::updateSelectedFeeds() {
if (SystemFactory::getInstance()->applicationCloseLock()->tryLockForRead()) {
emit feedsUpdateRequested(m_feedsView->selectedFeeds());
}
else {
qDebug("Lock for feed updates was NOT obtained.");
}
}
void FeedMessageViewer::onFeedUpdatesFinished() {
// Updates of some feeds finished, unlock the lock.
SystemFactory::getInstance()->applicationCloseLock()->unlock();
}
void FeedMessageViewer::createConnections() { void FeedMessageViewer::createConnections() {
// General connections. // General connections.
connect(m_messagesView, SIGNAL(currentMessageRemoved()), connect(m_messagesView, SIGNAL(currentMessageRemoved()),
@ -113,6 +143,17 @@ void FeedMessageViewer::createConnections() {
SIGNAL(triggered()), m_messagesView, SLOT(setAllMessagesUnread())); SIGNAL(triggered()), m_messagesView, SLOT(setAllMessagesUnread()));
connect(FormMain::getInstance()->m_ui->m_actionDeleteAllMessages, connect(FormMain::getInstance()->m_ui->m_actionDeleteAllMessages,
SIGNAL(triggered()), m_messagesView, SLOT(setAllMessagesDeleted())); SIGNAL(triggered()), m_messagesView, SLOT(setAllMessagesDeleted()));
connect(FormMain::getInstance()->m_ui->m_actionUpdateSelectedFeeds,
SIGNAL(triggered()), this, SLOT(updateSelectedFeeds()));
// Downloader connections.
// TODO: Připravit spojení pro progress a finished.
connect(m_feedDownloaderThread, SIGNAL(finished()),
m_feedDownloaderThread, SLOT(deleteLater()));
connect(this, SIGNAL(feedsUpdateRequested(QList<FeedsModelFeed*>)),
m_feedDownloader, SLOT(updateFeeds(QList<FeedsModelFeed*>)));
connect(m_feedDownloader, SIGNAL(finished()),
this, SLOT(onFeedUpdatesFinished()));
} }
void FeedMessageViewer::initialize() { void FeedMessageViewer::initialize() {
@ -135,6 +176,10 @@ void FeedMessageViewer::initialize() {
// Finish web/message browser setup. // Finish web/message browser setup.
m_messagesBrowser->setNavigationBarVisible(false); m_messagesBrowser->setNavigationBarVisible(false);
// Downloader setup.
qRegisterMetaType<QList<FeedsModelFeed*> >("QList<FeedsModelFeed*>");
m_feedDownloader->moveToThread(m_feedDownloaderThread);
} }
void FeedMessageViewer::initializeViews() { void FeedMessageViewer::initializeViews() {

View File

@ -10,6 +10,7 @@ class MessagesView;
class FeedDownloader; class FeedDownloader;
class QToolBar; class QToolBar;
class QSplitter; class QSplitter;
class FeedsModelFeed;
class FeedMessageViewer : public TabContent { class FeedMessageViewer : public TabContent {
Q_OBJECT Q_OBJECT
@ -28,6 +29,13 @@ class FeedMessageViewer : public TabContent {
void saveSize(); void saveSize();
void loadSize(); void loadSize();
// Destroys worker/feed downloader thread.
void quitDownloader();
public slots:
void updateSelectedFeeds();
void onFeedUpdatesFinished();
protected: protected:
// Initializes some properties of the widget. // Initializes some properties of the widget.
void initialize(); void initialize();
@ -38,6 +46,9 @@ class FeedMessageViewer : public TabContent {
// Sets up connections. // Sets up connections.
void createConnections(); void createConnections();
signals:
void feedsUpdateRequested(QList<FeedsModelFeed*>);
private: private:
QToolBar *m_toolBar; QToolBar *m_toolBar;
@ -48,6 +59,7 @@ class FeedMessageViewer : public TabContent {
FeedsView *m_feedsView; FeedsView *m_feedsView;
WebBrowser *m_messagesBrowser; WebBrowser *m_messagesBrowser;
QThread *m_feedDownloaderThread;
FeedDownloader *m_feedDownloader; FeedDownloader *m_feedDownloader;
}; };

View File

@ -16,11 +16,11 @@ class FeedsView : public QTreeView {
explicit FeedsView(QWidget *parent = 0); explicit FeedsView(QWidget *parent = 0);
virtual ~FeedsView(); virtual ~FeedsView();
// Enables or disables sorting.
void setSortingEnabled(bool enable); void setSortingEnabled(bool enable);
// Returns list of selected feeds. // Returns list of selected/all feeds.
QList<FeedsModelFeed*> selectedFeeds() const; QList<FeedsModelFeed*> selectedFeeds() const;
QList<FeedsModelFeed*> allFeeds() const; QList<FeedsModelFeed*> allFeeds() const;
public slots: public slots:
@ -31,12 +31,11 @@ class FeedsView : public QTreeView {
// Sets up appearance of this widget. // Sets up appearance of this widget.
void setupAppearance(); void setupAppearance();
void selectionChanged(const QItemSelection &selected, void selectionChanged(const QItemSelection &selected,
const QItemSelection &deselected); const QItemSelection &deselected);
signals: signals:
void feedsSelected(const QList<int> feed_ids); void feedsSelected(const QList<int> &feed_ids);
private: private:
FeedsModel *m_sourceModel; FeedsModel *m_sourceModel;

View File

@ -132,19 +132,6 @@ void FormMain::processExecutionMessage(const QString &message) {
void FormMain::quit() { void FormMain::quit() {
qDebug("Quitting the application."); qDebug("Quitting the application.");
// Make sure that we obtain close lock
// BEFORE even trying to quit the application.
if (SystemFactory::getInstance()->applicationCloseLock()->tryLockForWrite(CLOSE_LOCK_TIMEOUT)) {
// Application obtained permission to close
// in a safety way.
qDebug("Close lock obtained safely.");
}
else {
// Request for write lock timed-out. This means
// that some critical action can be processed right now.
qDebug("Close lock timed-out.");
}
qApp->quit(); qApp->quit();
} }
@ -181,20 +168,30 @@ void FormMain::display() {
void FormMain::onCommitData(QSessionManager &manager) { void FormMain::onCommitData(QSessionManager &manager) {
Q_UNUSED(manager) Q_UNUSED(manager)
qDebug("OS asked application to commit its data."); qDebug("OS asked application to commit its data.");
onAboutToQuit();
} }
void FormMain::onSaveState(QSessionManager &manager) { void FormMain::onSaveState(QSessionManager &manager) {
Q_UNUSED(manager) Q_UNUSED(manager)
qDebug("OS asked application to save its state."); qDebug("OS asked application to save its state.");
onAboutToQuit();
} }
void FormMain::onAboutToQuit() { void FormMain::onAboutToQuit() {
// Make sure that we obtain close lock
// BEFORE even trying to quit the application.
if (SystemFactory::getInstance()->applicationCloseLock()->tryLockForWrite(CLOSE_LOCK_TIMEOUT)) {
// Application obtained permission to close
// in a safety way.
qDebug("Close lock obtained safely.");
}
else {
// Request for write lock timed-out. This means
// that some critical action can be processed right now.
qDebug("Close lock timed-out.");
}
qDebug("Cleaning up resources and saving application state."); qDebug("Cleaning up resources and saving application state.");
m_ui->m_tabWidget->feedMessageViewer()->quitDownloader();
saveSize(); saveSize();
} }

View File

@ -89,7 +89,7 @@ int main(int argc, char *argv[]) {
window.setWindowTitle(APP_LONG_NAME); window.setWindowTitle(APP_LONG_NAME);
// Now is a good time to initialize dynamic keyboard shortcuts. // Now is a good time to initialize dynamic keyboard shortcuts.
DynamicShortcuts::load(FormMain::getInstance()->getActions()); DynamicShortcuts::load(window.getActions());
// Display welcome dialog if application is launched for the first time. // Display welcome dialog if application is launched for the first time.
if (Settings::getInstance()->value(APP_CFG_GEN, "first_start", true).toBool()) { if (Settings::getInstance()->value(APP_CFG_GEN, "first_start", true).toBool()) {