]> Some of my projects - launcher.git/commitdiff
Slight optimization & cleanup master
authorAPTX <marek321@gmail.com>
Mon, 5 Nov 2012 20:39:53 +0000 (21:39 +0100)
committerAPTX <marek321@gmail.com>
Mon, 5 Nov 2012 20:39:53 +0000 (21:39 +0100)
launcher/launcher.cpp
launcher/plugin.cpp
launcher/plugin.h
launcher/resultmodel.cpp
launcher/resultmodel.h
pluginapi/launcherpluginbase.h
plugins/localmylist/localmylistplugin.cpp
plugins/localmylist/localmylistplugin.h
plugins/test/test.cpp

index 1e2004dfc782657703dc08465016f0679e4e5529..36ff2239fea933d0b0c92ce9148915093f249489 100644 (file)
@@ -48,8 +48,8 @@ Launcher::Launcher(int argc, char **argv) :
        menu->addAction(quitAction);
        m_tray->setContextMenu(menu);
 
        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)), m_resultModel, SLOT(commandChanged(QString)));
+       connect(m_mainWindow, SIGNAL(commandChanged(QString)), this, SLOT(queryPlugins(QString)));
        m_tray->show();
 
 }
        m_tray->show();
 
 }
index c6c14880bb8173de94ace33550b408f6e1de42c3..001e46fc87732d0143601a37aa52d89199fb4710 100644 (file)
@@ -1,12 +1,19 @@
 #include "plugin.h"
 
 #include "pluginthread.h"
 #include "plugin.h"
 
 #include "pluginthread.h"
+#include "reply.h"
 #include "launcherpluginbase.h"
 
 #include "launcherpluginbase.h"
 
+#include <QDebug>
+
 Plugin::Plugin(const QString &file, QObject *parent) : QObject(parent),
 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();
 {
        unload();
+       if (m_previousResult)
+               delete m_previousResult;
+       if (m_latestResult)
+               delete m_latestResult;
 }
 
 bool Plugin::isCurrentResult() const
 }
 
 bool Plugin::isCurrentResult() const
@@ -19,12 +26,12 @@ Plugin::Status Plugin::status() const
        return m_status;
 }
 
        return m_status;
 }
 
-Reply Plugin::latestResult() const
+Reply *Plugin::latestResult() const
 {
        return m_latestResult;
 }
 
 {
        return m_latestResult;
 }
 
-Reply Plugin::previousResult() const
+Reply *Plugin::previousResult() const
 {
        return m_previousResult;
 }
 {
        return m_previousResult;
 }
@@ -40,7 +47,7 @@ bool Plugin::load()
                return false;
 
        bool r = m_pluginThread->startThread();
                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;
        m_status = r ? Loaded : Error;
 
        return r;
@@ -57,7 +64,10 @@ void Plugin::unload()
 void Plugin::processCommand(const QString &cmd)
 {
        if (m_lastProcessedCommand == cmd)
 void Plugin::processCommand(const QString &cmd)
 {
        if (m_lastProcessedCommand == cmd)
+       {
+               emit resultRecieved();
                return;
                return;
+       }
 
        m_latestCommand = cmd;
 
 
        m_latestCommand = cmd;
 
@@ -71,8 +81,10 @@ void Plugin::processCommand(const QString &cmd)
                                                          Q_ARG(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;
        m_previousResult = m_latestResult;
        m_latestResult = reply;
        executing = false;
index bfcafd737f4a3d79938e51efe437ae21fc69d9ec..af5d745f7d738e71b12ac4f1586b304176a7ce51 100644 (file)
@@ -3,8 +3,7 @@
 
 #include <QObject>
 
 
 #include <QObject>
 
-#include "reply.h"
-
+struct Reply;
 class PluginThread;
 class LauncherPluginBase;
 
 class PluginThread;
 class LauncherPluginBase;
 
@@ -22,8 +21,8 @@ public:
        
        bool isCurrentResult() const;
        Status status() const;
        
        bool isCurrentResult() const;
        Status status() const;
-       Reply latestResult() const;
-       Reply previousResult() const;
+       Reply *latestResult() const;
+       Reply *previousResult() const;
 
        LauncherPluginBase *pluginInstance();
 
 
        LauncherPluginBase *pluginInstance();
 
@@ -37,7 +36,7 @@ public slots:
        void processCommand(const QString &cmd);
 
 private slots:
        void processCommand(const QString &cmd);
 
 private slots:
-       void handleReply(const Reply &reply);
+       void handleReply(Reply *reply);
 
 private:
        bool executing;
 
 private:
        bool executing;
@@ -46,8 +45,8 @@ private:
        QString m_lastProcessedCommand;
        QString m_latestCommand;
 
        QString m_lastProcessedCommand;
        QString m_latestCommand;
 
-       Reply m_previousResult;
-       Reply m_latestResult;
+       Reply *m_previousResult;
+       Reply *m_latestResult;
 
        PluginThread *m_pluginThread;
 };
 
        PluginThread *m_pluginThread;
 };
index 3b1968980a8a93472a8fbca544f05e99c36ff168..2a4a8d60c57362dcff92249a4014e30cdf984f24 100644 (file)
@@ -122,7 +122,7 @@ void ResultModel::updateResults()
 
        if (idx == -1)
        {
 
        if (idx == -1)
        {
-               if (!plugin->latestResult().results.count())
+               if (!plugin->latestResult()->results.count())
                        return;
                pluginOrder << plugin;
        }
                        return;
                pluginOrder << plugin;
        }
@@ -136,9 +136,9 @@ void ResultModel::updateResults()
        {
                Plugin *p = pluginOrder[i];
 
        {
                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;
                        PluginResult *pr = new PluginResult;
                        pr->isCurrent = true;
                        pr->plugin = p;
@@ -148,83 +148,8 @@ void ResultModel::updateResults()
        }
        endResetModel();
 
        }
        endResetModel();
 
-/*     Plugin *plugin = qobject_cast<Plugin*>(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);
        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();
 }
 }
index 00a26397855b5c4111dba9b9ef6c5d053125c314..abdf7f7beea81d763a82c9241a3684b73f226a64 100644 (file)
@@ -9,7 +9,7 @@ class Plugin;
 class ResultModel : public QAbstractListModel
 {
        Q_OBJECT
 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;
 public:
        struct PluginResult {
                bool isCurrent;
@@ -48,8 +48,6 @@ signals:
        void selectedIndexChanged(int idx);
 
 private:
        void selectedIndexChanged(int idx);
 
 private:
-       void updateResultsForRange(int start, int end);
-
        QList<Plugin*> pluginOrder;
        QList<PluginResult*> results;
        int rowCountDelta;
        QList<Plugin*> pluginOrder;
        QList<PluginResult*> results;
        int rowCountDelta;
index 52a60f6186863e608ea92cc0ea5f04a91e9bd976..e18523435440a5c9f7c7983016cdca8e9370f4dc 100644 (file)
@@ -29,7 +29,7 @@ public slots:
        void processCommand(const QString &cmd);
 
 signals:
        void processCommand(const QString &cmd);
 
 signals:
-       void replyReady(const Reply &match);
+       void replyReady(Reply *match);
        
 };
 
        
 };
 
index d2aed822054307de5cb65e0790c34a3170b331ad..e0f3b27ff8816e84c5380cfe28daa559e309dcf9 100644 (file)
@@ -35,56 +35,87 @@ QString LocalMyListPlugin::name() const
 
 void LocalMyListPlugin::handleCommand(const QString &cmd)
 {
 
 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);
 }
 
 
        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());
 {
        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();
 }
        }
        q.finish();
 }
index 4bc2b39025b1e1068743dc8923972683efcae748..c3adf67d3bdf5d2d8f7df4099fa79d719c3d1ef5 100644 (file)
@@ -18,7 +18,7 @@ public:
        void handleCommand(const QString &cmd);
 
 private:
        void handleCommand(const QString &cmd);
 
 private:
-       void addResults(Reply &re, QSqlQuery &q);
+       void addResults(Reply *re, QSqlQuery &q);
 };
 
 #endif // LOCALMYLIST_H
 };
 
 #endif // LOCALMYLIST_H
index e1a5b6f29788481bd9a4d1ac16f72bb843e7321a..aeee01e9da6fa63151b6fa8267416ce682e15e40 100644 (file)
@@ -11,7 +11,7 @@ QString Test::name() const
 
 void Test::handleCommand(const QString &cmd)
 {
 
 void Test::handleCommand(const QString &cmd)
 {
-       Reply re;
+       Reply *re = new Reply;
        if (cmd.isEmpty())
        {
                emit replyReady(re);
        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"));
 
 
        Result r(cmd, "Echo for " + cmd + " z=" + QString::number(z), RunCommand, "cmd", QStringList("dir"));
 
-       re.addResult(r);
+       re->addResult(r);
        r.match += " 1";
        r.match += " 1";
-       re.addResult(r);
+       re->addResult(r);
        r.match += " 2";
        r.match += " 2";
-       re.addResult(r);
+       re->addResult(r);
        emit replyReady(re);
 }
 
        emit replyReady(re);
 }