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 TRAY_ICON_BUBBLE_TIMEOUT 15000
#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_SPLIT "-- !\n"

View File

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

View File

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

View File

@ -34,6 +34,12 @@ class FeedsModelFeed : public FeedsModelRootItem {
int countOfUnreadMessages() const;
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.
Type type() const;
void setType(const Type &type);

View File

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

View File

@ -34,12 +34,6 @@ class FeedsModelRootItem {
virtual int countOfUnreadMessages() 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;
// 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 *category = new FeedsModelStandardCategory(NULL);

View File

@ -21,9 +21,6 @@ class FeedsModelStandardCategory : public FeedsModelCategory {
// Returns the actual data representation of standard category.
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.
static FeedsModelStandardCategory *loadFromRecord(const QSqlRecord &record);
};

View File

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

View File

@ -3,6 +3,8 @@
#include "core/settings.h"
#include "core/messagesproxymodel.h"
#include "core/feeddownloader.h"
#include "core/feedsmodelstandardfeed.h"
#include "core/systemfactory.h"
#include "gui/webbrowser.h"
#include "gui/formmain.h"
#include "gui/iconthemefactory.h"
@ -18,6 +20,8 @@
#include <QToolButton>
#include <QMenu>
#include <QWidgetAction>
#include <QThread>
#include <QReadWriteLock>
FeedMessageViewer::FeedMessageViewer(QWidget *parent)
@ -25,10 +29,15 @@ FeedMessageViewer::FeedMessageViewer(QWidget *parent)
m_toolBar(new QToolBar(tr("Toolbar for messages"), this)),
m_messagesView(new MessagesView(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();
initializeViews();
createConnections();
// Start the feed downloader thread.
m_feedDownloaderThread->start();
}
FeedMessageViewer::~FeedMessageViewer() {
@ -75,6 +84,27 @@ void FeedMessageViewer::loadSize() {
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() {
// General connections.
connect(m_messagesView, SIGNAL(currentMessageRemoved()),
@ -113,6 +143,17 @@ void FeedMessageViewer::createConnections() {
SIGNAL(triggered()), m_messagesView, SLOT(setAllMessagesUnread()));
connect(FormMain::getInstance()->m_ui->m_actionDeleteAllMessages,
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() {
@ -135,6 +176,10 @@ void FeedMessageViewer::initialize() {
// Finish web/message browser setup.
m_messagesBrowser->setNavigationBarVisible(false);
// Downloader setup.
qRegisterMetaType<QList<FeedsModelFeed*> >("QList<FeedsModelFeed*>");
m_feedDownloader->moveToThread(m_feedDownloaderThread);
}
void FeedMessageViewer::initializeViews() {

View File

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

View File

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

View File

@ -132,19 +132,6 @@ void FormMain::processExecutionMessage(const QString &message) {
void FormMain::quit() {
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();
}
@ -181,20 +168,30 @@ void FormMain::display() {
void FormMain::onCommitData(QSessionManager &manager) {
Q_UNUSED(manager)
qDebug("OS asked application to commit its data.");
onAboutToQuit();
}
void FormMain::onSaveState(QSessionManager &manager) {
Q_UNUSED(manager)
qDebug("OS asked application to save its state.");
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.");
m_ui->m_tabWidget->feedMessageViewer()->quitDownloader();
saveSize();
}

View File

@ -89,7 +89,7 @@ int main(int argc, char *argv[]) {
window.setWindowTitle(APP_LONG_NAME);
// 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.
if (Settings::getInstance()->value(APP_CFG_GEN, "first_start", true).toBool()) {