]> Some of my projects - localmylist.git/commitdiff
When getting a file by path, try mapping the path to a source.
authorAPTX <marek321@gmail.com>
Sun, 14 Apr 2013 14:54:00 +0000 (16:54 +0200)
committerAPTX <marek321@gmail.com>
Sun, 14 Apr 2013 14:54:00 +0000 (16:54 +0200)
localmylist/database.cpp
localmylist/mylist.cpp
localmylist/mylist.h

index 8c9c539ec43d2e001219b3832cd7b78f99462d48..aba3988167da6615502ce77d99c78981fe9b3263 100644 (file)
@@ -453,6 +453,7 @@ File Database::getFile(int fid)
 File Database::getFileByPath(const QString &path)
 {
        File f;
+       QString mappedPath = MyList::instance()->mapPathToSource(path);
 
        QSqlQuery &q = prepare(
        "SELECT f.fid, f.eid, f.aid, f.gid, f.entry_added, f.anidb_update, f.entry_update, f.my_update, "
@@ -463,7 +464,7 @@ File Database::getFileByPath(const QString &path)
        "       FROM file f "
        "       JOIN file_location fl ON (fl.fid = f.fid) "
        "       WHERE fl.path = :path ");
-       q.bindValue(":path", path);
+       q.bindValue(":path", mappedPath);
 
        if (!exec(q))
                return File();
@@ -1569,7 +1570,7 @@ OpenFileData Database::readOpenFileData(QSqlQuery &q)
 
                data.hostId = q.value(5).toInt();
                data.localPath = q.value(4).toString();
-               data.path = MyList::instance()->mapPath(data.hostId, data.localPath);
+               data.path = MyList::instance()->mapPathFromSource(data.localPath, data.hostId);
 
                if (!data.path.isEmpty())
                        break;
index 097e7ceb8e512ff3857f2b54b1710aed8d10ddf4..b4636bffc8415f778b5da2747a7cfc8d2ac53786 100644 (file)
@@ -284,7 +284,7 @@ void MyList::executeTask(AbstractTask *task)
        emit taskCountChanged();
 }
 
-QString MyList::mapPath(int sourceHost, const QString &path)
+QString MyList::mapPathFromSource(const QString &path, int sourceHost)
 {
        if (sourceHost == hostId())
                return path;
@@ -314,6 +314,37 @@ QString MyList::mapPath(int sourceHost, const QString &path)
        return QString();
 }
 
+QString MyList::mapPathToSource(const QString &path, int *sourceHost)
+{
+       QList<PathMapping> mappings = db->getMappingsToHost(hostId());
+
+       foreach(const PathMapping &pm, mappings)
+       {
+               QString prefix = pm.destinationPrefix;
+               if (!prefix.endsWith(QChar('/')))
+                       prefix.append(QChar('/'));
+
+               if (!path.startsWith(prefix))
+                       continue;
+
+               QString ret = pm.sourcePrefix;
+
+               if (!ret.endsWith(QChar('/')))
+                       ret.append(QChar('/'));
+               ret.append(path.mid(prefix.length()));
+
+               if (sourceHost)
+                       *sourceHost = pm.sourceHost;
+
+               return ret;
+       }
+
+       if (sourceHost)
+               *sourceHost = hostId();
+
+       return path;
+}
+
 void MyList::taskFinished()
 {
        AbstractTask *task = qobject_cast<AbstractTask *>(sender());
index 63b7a315e4495c5f67ae6a4a4565eded9698e8df..f427ed0e98c8b52a2d5639e075e2f95882d114f3 100644 (file)
@@ -58,7 +58,8 @@ public slots:
        AbstractTask *importMyList(const QFileInfo &file);
        void executeTask(AbstractTask *task);
 
-       QString mapPath(int sourceHost, const QString &path);
+       QString mapPathFromSource(const QString &path, int sourceHost);
+       QString mapPathToSource(const QString &path, int *sourceHost = 0);
 
        void setupUdpClient();
        void setupRequestHandler();