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();
}
#include "plugin.h"
#include "pluginthread.h"
+#include "reply.h"
#include "launcherpluginbase.h"
+#include <QDebug>
+
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
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;
}
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;
void Plugin::processCommand(const QString &cmd)
{
if (m_lastProcessedCommand == cmd)
+ {
+ emit resultRecieved();
return;
+ }
m_latestCommand = 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;
#include <QObject>
-#include "reply.h"
-
+struct Reply;
class PluginThread;
class LauncherPluginBase;
bool isCurrentResult() const;
Status status() const;
- Reply latestResult() const;
- Reply previousResult() const;
+ Reply *latestResult() const;
+ Reply *previousResult() const;
LauncherPluginBase *pluginInstance();
void processCommand(const QString &cmd);
private slots:
- void handleReply(const Reply &reply);
+ void handleReply(Reply *reply);
private:
bool executing;
QString m_lastProcessedCommand;
QString m_latestCommand;
- Reply m_previousResult;
- Reply m_latestResult;
+ Reply *m_previousResult;
+ Reply *m_latestResult;
PluginThread *m_pluginThread;
};
if (idx == -1)
{
- if (!plugin->latestResult().results.count())
+ if (!plugin->latestResult()->results.count())
return;
pluginOrder << plugin;
}
{
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;
}
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);
-}
-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();
}
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;
void selectedIndexChanged(int idx);
private:
- void updateResultsForRange(int start, int end);
-
QList<Plugin*> pluginOrder;
QList<PluginResult*> results;
int rowCountDelta;
void processCommand(const QString &cmd);
signals:
- void replyReady(const Reply &match);
+ void replyReady(Reply *match);
};
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();
}
void handleCommand(const QString &cmd);
private:
- void addResults(Reply &re, QSqlQuery &q);
+ void addResults(Reply *re, QSqlQuery &q);
};
#endif // LOCALMYLIST_H
void Test::handleCommand(const QString &cmd)
{
- Reply re;
+ Reply *re = new Reply;
if (cmd.isEmpty())
{
emit replyReady(re);
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);
}