diff --git a/src/imagefoldermodel.cpp b/src/imagefoldermodel.cpp index 6fe903e..524bce5 100644 --- a/src/imagefoldermodel.cpp +++ b/src/imagefoldermodel.cpp @@ -1,165 +1,165 @@ /* * Copyright 2017 by Marco Martin * Copyright (C) 2017 Atul Sharma * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details * * You should have received a copy of the GNU Library General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "imagefoldermodel.h" #include "types.h" #include "roles.h" #include #include #include #include #include #include -#include +#include #include #include ImageFolderModel::ImageFolderModel(QObject *parent) : KDirModel(parent) { QMimeDatabase db; QList mimeList = db.allMimeTypes(); m_mimeTypes << "inode/directory"; foreach (const QMimeType &mime, mimeList) { if (mime.name().startsWith(QStringLiteral("image/"))) { m_mimeTypes << mime.name(); } } dirLister()->setMimeFilter(m_mimeTypes); connect(this, &QAbstractItemModel::rowsInserted, this, &ImageFolderModel::countChanged); connect(this, &QAbstractItemModel::rowsRemoved, this, &ImageFolderModel::countChanged); connect(this, &QAbstractItemModel::modelReset, this, &ImageFolderModel::countChanged); - - connect(this, &ImageFolderModel::showImageViewer, - this, [this] (const QString path) {qDebug() << "will show image " << path;}); } ImageFolderModel::~ImageFolderModel() { } QHash ImageFolderModel::roleNames() const { return { { Qt::DisplayRole, "display" }, { Qt::DecorationRole, "decoration" }, { Roles::ImageUrlRole, "imageurl" }, { Roles::MimeTypeRole, "mimeType" }, { Roles::ItemTypeRole, "itemType"} }; } QString ImageFolderModel::url() const { return dirLister()->url().toString(); } -void ImageFolderModel::setUrl(const QString& url) +void ImageFolderModel::setUrl(QString& url) { if (url.isEmpty()) { return; } + Q_ASSERT( QUrl(url).isLocalFile()); + url = QUrl(url).path(); + QString directoryUrl; - if( !QFileInfo(url).isDir()) { - m_imagePath = url; - directoryUrl = url.left(url.lastIndexOf('/')); - emit showImageViewer( m_imagePath); + if( QDir(url).exists()) { + directoryUrl = QUrl::fromLocalFile(url).toString(); } else { - directoryUrl = "file://" + url; + m_imagePath = url; + directoryUrl = QUrl::fromLocalFile(url.left(url.lastIndexOf('/'))).toString(); + emit showImageViewer( indexForUrl(m_imagePath)); } if (dirLister()->url().path() == directoryUrl) { dirLister()->updateDirectory(QUrl(directoryUrl)); return; } beginResetModel(); dirLister()->openUrl(QUrl(directoryUrl)); endResetModel(); emit urlChanged(); } int ImageFolderModel::indexForUrl(const QString &url) const { QModelIndex index = KDirModel::indexForUrl(QUrl(url)); return index.row(); } QVariantMap ImageFolderModel::get(int i) const { QModelIndex modelIndex = index(i, 0); KFileItem item = itemForIndex(modelIndex); QString url = item.url().toString(); QString mimeType = item.mimetype(); QVariantMap ret; ret.insert(QStringLiteral("url"), QVariant(url)); ret.insert(QStringLiteral("mimeType"), QVariant(mimeType)); return ret; } void ImageFolderModel::emptyTrash() { KIO::emptyTrash(); } QVariant ImageFolderModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) { return QVariant(); } switch (role) { case Roles::ImageUrlRole: { KFileItem item = itemForIndex(index); return item.url().toString(); } case Roles::MimeTypeRole: { KFileItem item = itemForIndex(index); return item.mimetype(); } case Roles::ItemTypeRole: { KFileItem item = itemForIndex(index); if( item.isDir()) { return Types::Folder; } else { return Types::Image; } } default: return KDirModel::data(index, role); } } #include "moc_imagefoldermodel.cpp" diff --git a/src/imagefoldermodel.h b/src/imagefoldermodel.h index ac8f551..aef9be9 100644 --- a/src/imagefoldermodel.h +++ b/src/imagefoldermodel.h @@ -1,81 +1,81 @@ /* * Copyright 2017 by Marco Martin * Copyright (C) 2017 Atul Sharma * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details * * You should have received a copy of the GNU Library General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef IMAGEFOLDERMODEL_H #define IMAGEFOLDERMODEL_H #include #include #include class QTimer; /** * This class provides a QML binding to KDirModel * Provides an easy way to navigate a filesystem from within QML * * @author Marco Martin */ class ImageFolderModel : public KDirModel { Q_OBJECT /** * @property string The url we want to browse. it may be an absolute path or a correct url of any protocol KIO supports */ Q_PROPERTY(QString url READ url WRITE setUrl NOTIFY urlChanged) /** * @property count Total number of rows */ Q_PROPERTY(int count READ count NOTIFY countChanged) public: ImageFolderModel(QObject* parent=0); virtual ~ImageFolderModel(); QHash roleNames() const override; - void setUrl(const QString& url); + void setUrl(QString& url); QString url() const; QVariant data(const QModelIndex &index, int role) const; int count() const {return rowCount();} Q_INVOKABLE int indexForUrl(const QString &url) const; Q_INVOKABLE QVariantMap get(int index) const; /** * Helper method to empty the trash */ Q_INVOKABLE void emptyTrash(); Q_SIGNALS: void countChanged(); void urlChanged(); - void showImageViewer(const QString &path); + void showImageViewer(int indexValue); private: QStringList m_mimeTypes; QString m_imagePath; }; #endif // IMAGEFOLDERMODEL_H diff --git a/src/main.cpp b/src/main.cpp index 821b05f..dcfc3c4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,129 +1,133 @@ /* * Copyright (C) 2017 Atul Sharma * Copyright (C) 2014 Vishesh Handa * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "filesystemtracker.h" #include "processor.h" #include "kokoconfig.h" #include "imagestorage.h" int main(int argc, char** argv) { QApplication app(argc, argv); app.setApplicationDisplayName("Koko"); app.setOrganizationDomain("kde.org"); KDBusService service(KDBusService::Unique); QCommandLineParser parser; parser.addOption(QCommandLineOption("reset", i18n("Reset the database"))); parser.addPositionalArgument( "image", i18n("path of image you want to open")); parser.addHelpOption(); parser.process(app); if (parser.positionalArguments().size() > 1) { parser.showHelp(1); } if (parser.isSet("reset")) { KokoConfig config; config.reset(); ImageStorage::reset(); } QThread trackerThread; QStringList locations = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation); Q_ASSERT(locations.size() >= 1); qDebug() << locations; QUrl currentDirPath = QUrl::fromLocalFile(QDir::currentPath().append('/')); QUrl resolvedImagePath = parser.positionalArguments().isEmpty() - ? QUrl(locations.first().append('/')) + ? QUrl::fromLocalFile(locations.first().append('/')) : currentDirPath.resolved( parser.positionalArguments().first()); + if( !resolvedImagePath.isLocalFile()) { + resolvedImagePath = QUrl::fromLocalFile(locations.first().append('/')) ; + } + FileSystemTracker tracker; tracker.setFolder(locations.first()); tracker.moveToThread(&trackerThread); Koko::Processor processor; QObject::connect(&tracker, &FileSystemTracker::imageAdded, &processor, &Koko::Processor::addFile); QObject::connect(&tracker, &FileSystemTracker::imageRemoved, &processor, &Koko::Processor::removeFile); QObject::connect(&tracker, &FileSystemTracker::initialScanComplete, &processor, &Koko::Processor::initialScanCompleted); trackerThread.start(); tracker.setSubFolder(tracker.folder()); KokoConfig config; QQmlEngine engine; QQmlContext* objectContext = engine.rootContext(); objectContext->setContextProperty("kokoProcessor", &processor); objectContext->setContextProperty("kokoConfig", &config); objectContext->setContextProperty("imagePathArgument", resolvedImagePath.toString()); QString path; //we want different main files on desktop or mobile //very small difference as they as they are subclasses of the same thing if (qEnvironmentVariableIsSet("QT_QUICK_CONTROLS_MOBILE") && (QString::fromLatin1(qgetenv("QT_QUICK_CONTROLS_MOBILE")) == QStringLiteral("1") || QString::fromLatin1(qgetenv("QT_QUICK_CONTROLS_MOBILE")) == QStringLiteral("true"))) { path = QStandardPaths::locate(QStandardPaths::DataLocation, "ui/mobileMain.qml"); } else { path = QStandardPaths::locate(QStandardPaths::DataLocation, "ui/desktopMain.qml"); } QQuickView* view = new QQuickView( &engine, new QWindow()); view->engine()->rootContext()->setContextObject(new KLocalizedContext(view)); QQmlComponent component(&engine, path); if (component.isError()) { std::cout << component.errorString().toUtf8().constData() << std::endl; Q_ASSERT(0); } Q_ASSERT(component.status() == QQmlComponent::Ready); QObject* obj = component.create(objectContext); Q_ASSERT(obj); int rt = app.exec(); trackerThread.quit(); return rt; }