From: APTX Date: Mon, 5 Nov 2012 20:39:53 +0000 (+0100) Subject: Slight optimization & cleanup X-Git-Url: https://gitweb.tyo.aptx.org/?a=commitdiff_plain;p=launcher.git Slight optimization & cleanup --- diff --git a/launcher/launcher.cpp b/launcher/launcher.cpp index 1e2004d..36ff223 100644 --- a/launcher/launcher.cpp +++ b/launcher/launcher.cpp @@ -48,8 +48,8 @@ Launcher::Launcher(int argc, char **argv) : menu->addAction(quitAction); m_tray->setContextMenu(menu); - connect(m_mainWindow, SIGNAL(commandChanged(QString)), this, SLOT(queryPlugins(QString))); connect(m_mainWindow, SIGNAL(commandChanged(QString)), m_resultModel, SLOT(commandChanged(QString))); + connect(m_mainWindow, SIGNAL(commandChanged(QString)), this, SLOT(queryPlugins(QString))); m_tray->show(); } diff --git a/launcher/plugin.cpp b/launcher/plugin.cpp index c6c1488..001e46f 100644 --- a/launcher/plugin.cpp +++ b/launcher/plugin.cpp @@ -1,12 +1,19 @@ #include "plugin.h" #include "pluginthread.h" +#include "reply.h" #include "launcherpluginbase.h" +#include + Plugin::Plugin(const QString &file, QObject *parent) : QObject(parent), - m_pluginThread(new PluginThread(file, this)), executing(false), m_status(Unloaded) + m_pluginThread(new PluginThread(file, this)), executing(false), m_status(Unloaded), m_latestResult(0), m_previousResult(0) { unload(); + if (m_previousResult) + delete m_previousResult; + if (m_latestResult) + delete m_latestResult; } bool Plugin::isCurrentResult() const @@ -19,12 +26,12 @@ Plugin::Status Plugin::status() const return m_status; } -Reply Plugin::latestResult() const +Reply *Plugin::latestResult() const { return m_latestResult; } -Reply Plugin::previousResult() const +Reply *Plugin::previousResult() const { return m_previousResult; } @@ -40,7 +47,7 @@ bool Plugin::load() return false; bool r = m_pluginThread->startThread(); - connect(m_pluginThread->plugin(), SIGNAL(replyReady(Reply)), this, SLOT(handleReply(Reply)), Qt::QueuedConnection); + connect(m_pluginThread->plugin(), SIGNAL(replyReady(Reply*)), this, SLOT(handleReply(Reply*)), Qt::QueuedConnection); m_status = r ? Loaded : Error; return r; @@ -57,7 +64,10 @@ void Plugin::unload() void Plugin::processCommand(const QString &cmd) { if (m_lastProcessedCommand == cmd) + { + emit resultRecieved(); return; + } m_latestCommand = cmd; @@ -71,8 +81,10 @@ void Plugin::processCommand(const QString &cmd) Q_ARG(QString, cmd)); } -void Plugin::handleReply(const Reply &reply) +void Plugin::handleReply(Reply *reply) { + if (m_previousResult) + delete m_previousResult; m_previousResult = m_latestResult; m_latestResult = reply; executing = false; diff --git a/launcher/plugin.h b/launcher/plugin.h index bfcafd7..af5d745 100644 --- a/launcher/plugin.h +++ b/launcher/plugin.h @@ -3,8 +3,7 @@ #include -#include "reply.h" - +struct Reply; class PluginThread; class LauncherPluginBase; @@ -22,8 +21,8 @@ public: bool isCurrentResult() const; Status status() const; - Reply latestResult() const; - Reply previousResult() const; + Reply *latestResult() const; + Reply *previousResult() const; LauncherPluginBase *pluginInstance(); @@ -37,7 +36,7 @@ public slots: void processCommand(const QString &cmd); private slots: - void handleReply(const Reply &reply); + void handleReply(Reply *reply); private: bool executing; @@ -46,8 +45,8 @@ private: QString m_lastProcessedCommand; QString m_latestCommand; - Reply m_previousResult; - Reply m_latestResult; + Reply *m_previousResult; + Reply *m_latestResult; PluginThread *m_pluginThread; }; diff --git a/launcher/resultmodel.cpp b/launcher/resultmodel.cpp index 3b19689..2a4a8d6 100644 --- a/launcher/resultmodel.cpp +++ b/launcher/resultmodel.cpp @@ -122,7 +122,7 @@ void ResultModel::updateResults() if (idx == -1) { - if (!plugin->latestResult().results.count()) + if (!plugin->latestResult()->results.count()) return; pluginOrder << plugin; } @@ -136,9 +136,9 @@ void ResultModel::updateResults() { Plugin *p = pluginOrder[i]; - for (int j = 0; j < p->latestResult().results.count(); ++j) + for (int j = 0; j < p->latestResult()->results.count(); ++j) { - Result r = p->latestResult().results[j]; + Result r = p->latestResult()->results[j]; PluginResult *pr = new PluginResult; pr->isCurrent = true; pr->plugin = p; @@ -148,83 +148,8 @@ void ResultModel::updateResults() } endResetModel(); -/* Plugin *plugin = qobject_cast(sender()); - auto it = pluginOrder.find(plugin); - int pluginIdx; - int resultIdx; - int oldResultCount; - int newResultCount; - - if (it == resultMap.end()) - { - if (!plugin->latestResult().results.count()) - return; - - pluginOrder.append(plugin); - - pluginIdx = pluginOrder.count(); - - PluginResult *pr = new PluginResult; - pr->plugin = plugin; - - results.append(pr); - resultMap.insert(plugin, pr); - - pluginIdx = results.count() - 1; - resultIdx = rowCount(); - oldResultCount = 0; - } - - if (it != resultMap.end()) - { - pluginIdx = results.indexOf(it.value()); - resultIdx = 0; - for (int i = 0; i < pluginIdx; ++i) - resultIdx += results[i]->count(); - - oldResultCount = plugin->previousResult().results.count(); - } - - newResultCount = plugin->latestResult().results.count(); - - results[pluginIdx]->isCurrent = plugin->isCurrentResult(); - - if (oldResultCount == newResultCount) - { -qDebug() << "Updating rows" << newResultCount; - - if (newResultCount) - updateResultsForRange(resultIdx, newResultCount); - } - else if(oldResultCount < newResultCount) - { -qDebug() << "Adding rows"; - if (oldResultCount) - updateResultsForRange(resultIdx, oldResultCount); -// rowCountDelta = -(newResultCount - oldResultCount); - beginInsertRows(QModelIndex(), resultIdx + oldResultCount, resultIdx + newResultCount - 1); - rowCountDelta = 0; - endInsertRows(); - } - else - { -qDebug() << "Removing rows"; -// rowCountDelta = oldResultCount - newResultCount; - beginRemoveRows(QModelIndex(), resultIdx + newResultCount, resultIdx + oldResultCount - 1); - rowCountDelta = 0; - endRemoveRows(); - if (newResultCount) - updateResultsForRange(resultIdx, newResultCount); - } -*/ if (oldCount != rowCount()) setSelectedIndex(0); -} -void ResultModel::updateResultsForRange(int start, int end) -{ -qDebug() << "Data changed" << start << (start+end-1); - QModelIndex sidx = index(start, 0); - QModelIndex eidx = index(start + end - 1, 0); - emit dataChanged(sidx, eidx); + qDebug() << "result count" << rowCount(); } diff --git a/launcher/resultmodel.h b/launcher/resultmodel.h index 00a2639..abdf7f7 100644 --- a/launcher/resultmodel.h +++ b/launcher/resultmodel.h @@ -9,7 +9,7 @@ class Plugin; class ResultModel : public QAbstractListModel { Q_OBJECT - Q_PROPERTY(int selectedIndex READ selectedIndex WRITE setSelectedIndex NOTIFY selectedIndexChanged()) + Q_PROPERTY(int selectedIndex READ selectedIndex WRITE setSelectedIndex NOTIFY selectedIndexChanged) public: struct PluginResult { bool isCurrent; @@ -48,8 +48,6 @@ signals: void selectedIndexChanged(int idx); private: - void updateResultsForRange(int start, int end); - QList pluginOrder; QList results; int rowCountDelta; diff --git a/pluginapi/launcherpluginbase.h b/pluginapi/launcherpluginbase.h index 52a60f6..e185234 100644 --- a/pluginapi/launcherpluginbase.h +++ b/pluginapi/launcherpluginbase.h @@ -29,7 +29,7 @@ public slots: void processCommand(const QString &cmd); signals: - void replyReady(const Reply &match); + void replyReady(Reply *match); }; diff --git a/plugins/localmylist/localmylistplugin.cpp b/plugins/localmylist/localmylistplugin.cpp index d2aed82..e0f3b27 100644 --- a/plugins/localmylist/localmylistplugin.cpp +++ b/plugins/localmylist/localmylistplugin.cpp @@ -35,56 +35,87 @@ QString LocalMyListPlugin::name() const void LocalMyListPlugin::handleCommand(const QString &cmd) { - Reply re; - - QSqlQuery &q = LocalMyList::instance()->database()->prepare( - "SELECT DISTINCT a.aid, b.title AS MainTitle, a.title AS searchTitle FROM anime_title a" - " LEFT JOIN anime_title b on b.aid = a.aid " -// " LEFT JOIN episode e ON e.aid = a.aid " -// " LEFT JOIN file f ON f.aid = a.aid " -// " LEFT JOIN file_location fl ON fl.fid = f.fid " - " WHERE lower(a.title) LIKE :title " - " AND b.type = 1 " - " ORDER BY a.title ASC, a.aid ASC " - ); - - q.bindValue(":title", cmd); - if (!LocalMyList::instance()->database()->exec(q)) + Reply *re = new Reply; + QString title(cmd); + title.replace(QRegExp("[_%]"), QString("")); + { - emit replyReady(re); - return; + QSqlQuery &q = LocalMyList::instance()->database()->prepare( + "SELECT DISTINCT at.aid, atb.title AS MainTitle, at.title AS searchTitle FROM anime_title at" + " JOIN anime_title atb on atb.aid = at.aid " + " JOIN anime a on at.aid = a.aid " +// " LEFT JOIN episode e ON e.aid = at.aid " +// " LEFT JOIN file f ON f.aid = at.aid " +// " LEFT JOIN file_location fl ON fl.fid = f.fid " + " WHERE lower(at.title) LIKE :title " + " AND atb.type = 1 " + " ORDER BY at.title ASC, at.aid ASC " + ); + + q.bindValue(":title", title); + if (!LocalMyList::instance()->database()->exec(q)) + { + emit replyReady(re); + return; + } + addResults(re, q); } - addResults(re, q); - q.bindValue(":title", cmd + "%"); - if (!LocalMyList::instance()->database()->exec(q)) { - emit replyReady(re); - return; + QSqlQuery &q = LocalMyList::instance()->database()->prepare( + "SELECT DISTINCT at.aid, atb.title AS MainTitle, at.title AS searchTitle FROM anime_title at" + " JOIN anime_title atb on atb.aid = at.aid " + " JOIN anime a on at.aid = a.aid " + " WHERE lower(at.title) LIKE :title " + " AND lower(at.title) NOT LIKE :title2 " + " AND atb.type = 1 " + " ORDER BY at.title ASC, at.aid ASC " + ); + q.bindValue(":title", title + "%"); + q.bindValue(":title2", title); + if (!LocalMyList::instance()->database()->exec(q)) + { + emit replyReady(re); + return; + } + addResults(re, q); } - addResults(re, q); - q.bindValue(":title", "%" + cmd + "%"); - if (!LocalMyList::instance()->database()->exec(q)) { - emit replyReady(re); - return; + QSqlQuery &q = LocalMyList::instance()->database()->prepare( + "SELECT DISTINCT at.aid, atb.title AS MainTitle, at.title AS searchTitle FROM anime_title at" + " JOIN anime_title atb on atb.aid = at.aid " + " JOIN anime a on at.aid = a.aid " + " WHERE lower(at.title) LIKE :title " + " AND lower(at.title) NOT LIKE :title2 " + " AND lower(at.title) NOT LIKE :title3 " + " AND atb.type = 1 " + " ORDER BY at.title ASC, at.aid ASC " + ); + q.bindValue(":title", "%" + title + "%"); + q.bindValue(":title2", title + "%"); + q.bindValue(":title3", title); + if (!LocalMyList::instance()->database()->exec(q)) + { + emit replyReady(re); + return; + } + addResults(re, q); } - addResults(re, q); emit replyReady(re); } -void LocalMyListPlugin::addResults(Reply &re, QSqlQuery &q) +void LocalMyListPlugin::addResults(Reply *re, QSqlQuery &q) { while(q.next()) { Result r; r.match = q.value(2).toString(); r.description = QString("Main title: %1").arg(q.value(1).toString()); -// r.commandType = OpenCommand; -// r.command = ofd.path; - re.addResult(r); + r.commandType = OpenCommand; + r.command = "http://anidb.net/a" + q.value(0).toString(); + re->addResult(r); } q.finish(); } diff --git a/plugins/localmylist/localmylistplugin.h b/plugins/localmylist/localmylistplugin.h index 4bc2b39..c3adf67 100644 --- a/plugins/localmylist/localmylistplugin.h +++ b/plugins/localmylist/localmylistplugin.h @@ -18,7 +18,7 @@ public: void handleCommand(const QString &cmd); private: - void addResults(Reply &re, QSqlQuery &q); + void addResults(Reply *re, QSqlQuery &q); }; #endif // LOCALMYLIST_H diff --git a/plugins/test/test.cpp b/plugins/test/test.cpp index e1a5b6f..aeee01e 100644 --- a/plugins/test/test.cpp +++ b/plugins/test/test.cpp @@ -11,7 +11,7 @@ QString Test::name() const void Test::handleCommand(const QString &cmd) { - Reply re; + Reply *re = new Reply; if (cmd.isEmpty()) { emit replyReady(re); @@ -23,11 +23,11 @@ void Test::handleCommand(const QString &cmd) Result r(cmd, "Echo for " + cmd + " z=" + QString::number(z), RunCommand, "cmd", QStringList("dir")); - re.addResult(r); + re->addResult(r); r.match += " 1"; - re.addResult(r); + re->addResult(r); r.match += " 2"; - re.addResult(r); + re->addResult(r); emit replyReady(re); }