From: APTX Date: Sun, 14 Apr 2013 14:54:00 +0000 (+0200) Subject: When getting a file by path, try mapping the path to a source. X-Git-Url: https://gitweb.tyo.aptx.org/?a=commitdiff_plain;h=40e2a7fb9b0e9aeeb0fa69ef0df1d8186574614a;p=localmylist.git When getting a file by path, try mapping the path to a source. --- diff --git a/localmylist/database.cpp b/localmylist/database.cpp index 8c9c539..aba3988 100644 --- a/localmylist/database.cpp +++ b/localmylist/database.cpp @@ -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; diff --git a/localmylist/mylist.cpp b/localmylist/mylist.cpp index 097e7ce..b4636bf 100644 --- a/localmylist/mylist.cpp +++ b/localmylist/mylist.cpp @@ -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 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(sender()); diff --git a/localmylist/mylist.h b/localmylist/mylist.h index 63b7a31..f427ed0 100644 --- a/localmylist/mylist.h +++ b/localmylist/mylist.h @@ -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();