Improve startup speed and use single instance process
							parent
							
								
									8418faf167
								
							
						
					
					
						commit
						6d73bb234a
					
				@ -0,0 +1,143 @@
 | 
				
			|||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Copyright (C) 2021 CutefishOS Team.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Author:     Reion Wong <reion@cutefishos.com>
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * This program is free software: you can redistribute it and/or modify
 | 
				
			||||||
 | 
					 * it under the terms of the GNU General Public License as published by
 | 
				
			||||||
 | 
					 * the Free Software Foundation, either version 3 of the License, or
 | 
				
			||||||
 | 
					 * 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 General Public License for more details.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * You should have received a copy of the GNU General Public License
 | 
				
			||||||
 | 
					 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "application.h"
 | 
				
			||||||
 | 
					#include "window.h"
 | 
				
			||||||
 | 
					#include "desktop/desktop.h"
 | 
				
			||||||
 | 
					#include "thumbnailer/thumbnailprovider.h"
 | 
				
			||||||
 | 
					#include "filemanageradaptor.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <QCommandLineParser>
 | 
				
			||||||
 | 
					#include <QQmlApplicationEngine>
 | 
				
			||||||
 | 
					#include <QQmlContext>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <QDBusConnection>
 | 
				
			||||||
 | 
					#include <QPixmapCache>
 | 
				
			||||||
 | 
					#include <QTranslator>
 | 
				
			||||||
 | 
					#include <QIcon>
 | 
				
			||||||
 | 
					#include <QDir>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Application::Application(int& argc, char** argv)
 | 
				
			||||||
 | 
					    : QApplication(argc, argv)
 | 
				
			||||||
 | 
					    , m_instance(false)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    if (QDBusConnection::sessionBus().registerService("com.cutefish.FileManager")) {
 | 
				
			||||||
 | 
					        setOrganizationName("cutefishos");
 | 
				
			||||||
 | 
					        setWindowIcon(QIcon::fromTheme("file-manager"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        new FileManagerAdaptor(this);
 | 
				
			||||||
 | 
					        QDBusConnection::sessionBus().registerObject("/FileManager", this);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Translations
 | 
				
			||||||
 | 
					        QLocale locale;
 | 
				
			||||||
 | 
					        QString qmFilePath = QString("%1/%2.qm").arg("/usr/share/cutefish-filemanager/translations/").arg(locale.name());
 | 
				
			||||||
 | 
					        if (QFile::exists(qmFilePath)) {
 | 
				
			||||||
 | 
					            QTranslator *translator = new QTranslator(this);
 | 
				
			||||||
 | 
					            if (translator->load(qmFilePath)) {
 | 
				
			||||||
 | 
					                installTranslator(translator);
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                translator->deleteLater();
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        m_instance = true;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int Application::run()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    if (!parseCommandLineArgs())
 | 
				
			||||||
 | 
					        return 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return QApplication::exec();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void Application::openFiles(const QStringList &paths)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    for (const QString &path : paths) {
 | 
				
			||||||
 | 
					        openWindow(path);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void Application::emptyTrash()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    Window *w = new Window;
 | 
				
			||||||
 | 
					    w->load(QUrl("qrc:/qml/Dialogs/EmptyTrashDialog.qml"));
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void Application::openWindow(const QString &path)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    Window *w = new Window;
 | 
				
			||||||
 | 
					    w->rootContext()->setContextProperty("arg", path);
 | 
				
			||||||
 | 
					    w->addImageProvider("thumbnailer", new ThumbnailProvider());
 | 
				
			||||||
 | 
					    w->load(QUrl("qrc:/qml/main.qml"));
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bool Application::parseCommandLineArgs()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    QCommandLineParser parser;
 | 
				
			||||||
 | 
					    parser.setApplicationDescription(QStringLiteral("File Manager"));
 | 
				
			||||||
 | 
					    parser.addHelpOption();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    parser.addPositionalArgument("files", "Files", "[FILE1, FILE2,...]");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    QCommandLineOption desktopOption(QStringList() << "d" << "desktop" << "Desktop Mode");
 | 
				
			||||||
 | 
					    parser.addOption(desktopOption);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    QCommandLineOption emptyTrashOption(QStringList() << "e" << "empty-trash" << "Empty Trash");
 | 
				
			||||||
 | 
					    parser.addOption(emptyTrashOption);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    parser.process(arguments());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (m_instance) {
 | 
				
			||||||
 | 
					        QPixmapCache::setCacheLimit(2048);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (parser.isSet(desktopOption)) {
 | 
				
			||||||
 | 
					            Desktop desktop;
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            QStringList paths = parser.positionalArguments();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (paths.isEmpty()) {
 | 
				
			||||||
 | 
					                paths.append(QDir::currentPath());
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            openFiles(paths);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					        QDBusInterface iface("com.cutefish.FileManager",
 | 
				
			||||||
 | 
					                             "/FileManager",
 | 
				
			||||||
 | 
					                             "com.cutefish.FileManager",
 | 
				
			||||||
 | 
					                             QDBusConnection::sessionBus(), this);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (parser.isSet(emptyTrashOption)) {
 | 
				
			||||||
 | 
					            // Empty Dialog
 | 
				
			||||||
 | 
					            iface.call("emptyTrash");
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            QStringList paths = parser.positionalArguments();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (paths.isEmpty()) {
 | 
				
			||||||
 | 
					                paths.append(QDir::currentPath());
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            iface.call("openFiles", paths);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return m_instance;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -0,0 +1,48 @@
 | 
				
			|||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Copyright (C) 2021 CutefishOS Team.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Author:     Reion Wong <reion@cutefishos.com>
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * This program is free software: you can redistribute it and/or modify
 | 
				
			||||||
 | 
					 * it under the terms of the GNU General Public License as published by
 | 
				
			||||||
 | 
					 * the Free Software Foundation, either version 3 of the License, or
 | 
				
			||||||
 | 
					 * 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 General Public License for more details.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * You should have received a copy of the GNU General Public License
 | 
				
			||||||
 | 
					 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifndef APPLICATION_H
 | 
				
			||||||
 | 
					#define APPLICATION_H
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <QApplication>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Application : public QApplication
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    Q_OBJECT
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public:
 | 
				
			||||||
 | 
					    explicit Application(int& argc, char** argv);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    int run();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // DBus
 | 
				
			||||||
 | 
					    void openFiles(const QStringList &paths);
 | 
				
			||||||
 | 
					    void emptyTrash();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					private:
 | 
				
			||||||
 | 
					    void openWindow(const QString &path);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					private:
 | 
				
			||||||
 | 
					    bool parseCommandLineArgs();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					private:
 | 
				
			||||||
 | 
					    bool m_instance;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif // APPLICATION_H
 | 
				
			||||||
@ -0,0 +1,9 @@
 | 
				
			|||||||
 | 
					<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
 | 
				
			||||||
 | 
					<node>
 | 
				
			||||||
 | 
					  <interface name="com.cutefish.FileManager">
 | 
				
			||||||
 | 
					    <method name="openFiles">
 | 
				
			||||||
 | 
					      <arg type="as" direction="in"/>
 | 
				
			||||||
 | 
					    </method>
 | 
				
			||||||
 | 
					    <method name="emptyTrash"></method>
 | 
				
			||||||
 | 
					  </interface>
 | 
				
			||||||
 | 
					</node>
 | 
				
			||||||
@ -0,0 +1,49 @@
 | 
				
			|||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Copyright (C) 2021 CutefishOS Team.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Author:     Reion Wong <reion@cutefishos.com>
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * This program is free software: you can redistribute it and/or modify
 | 
				
			||||||
 | 
					 * it under the terms of the GNU General Public License as published by
 | 
				
			||||||
 | 
					 * the Free Software Foundation, either version 3 of the License, or
 | 
				
			||||||
 | 
					 * 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 General Public License for more details.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * You should have received a copy of the GNU General Public License
 | 
				
			||||||
 | 
					 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "window.h"
 | 
				
			||||||
 | 
					#include <QEvent>
 | 
				
			||||||
 | 
					#include <QDebug>
 | 
				
			||||||
 | 
					#include <QQuickWindow>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Window::Window(QObject *parent)
 | 
				
			||||||
 | 
					    : QQmlApplicationEngine(parent)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void Window::load(const QUrl &url)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    QQmlApplicationEngine::load(url);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    QQuickWindow *w = qobject_cast<QQuickWindow *>(rootObjects().first());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (w)
 | 
				
			||||||
 | 
					        w->installEventFilter(this);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bool Window::eventFilter(QObject *obj, QEvent *e)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    if (e->type() == QEvent::Close) {
 | 
				
			||||||
 | 
					        clearComponentCache();
 | 
				
			||||||
 | 
					        deleteLater();
 | 
				
			||||||
 | 
					        e->accept();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return QObject::eventFilter(obj, e);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -0,0 +1,38 @@
 | 
				
			|||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Copyright (C) 2021 CutefishOS Team.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Author:     Reion Wong <reion@cutefishos.com>
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * This program is free software: you can redistribute it and/or modify
 | 
				
			||||||
 | 
					 * it under the terms of the GNU General Public License as published by
 | 
				
			||||||
 | 
					 * the Free Software Foundation, either version 3 of the License, or
 | 
				
			||||||
 | 
					 * 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 General Public License for more details.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * You should have received a copy of the GNU General Public License
 | 
				
			||||||
 | 
					 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifndef WINDOW_H
 | 
				
			||||||
 | 
					#define WINDOW_H
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <QQmlApplicationEngine>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Window : public QQmlApplicationEngine
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    Q_OBJECT
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public:
 | 
				
			||||||
 | 
					    explicit Window(QObject *parent = nullptr);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    void load(const QUrl &url);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					protected:
 | 
				
			||||||
 | 
					    bool eventFilter(QObject *o, QEvent *e);
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif // WINDOW_H
 | 
				
			||||||
					Loading…
					
					
				
		Reference in New Issue