Commit 2f168d23 authored by Toni Asensi Esteve's avatar Toni Asensi Esteve
Browse files

Archives: Solve the 'QCoreApplication::applicationDirPath: Please instantiate...

Archives: Solve the 'QCoreApplication::applicationDirPath: Please instantiate the QApplication object first' error

Solve the «QCoreApplication::applicationDirPath: Please instantiate the QApplication object first» error, what required moving code.

Besides, after the code of the `generateKrarcArchiveMimetypes()` function was moved to a constructor, that code is executed only once and the source code ends up being simplified. Make the KrServices class simpler by moving archive-related code to an archive-related class.

Test plan: The one described in the task [Solve some bugs related to archives, remove some duplicated code](https://phabricator.kde.org/T12876).

FIXED: [ 417107 ] QCoreApplication::applicationDirPath: Please instantiate the QApplication object first
BUG: 417107

Differential Revision: https://phabricator.kde.org/D28413
parent 2f54e775
......@@ -209,7 +209,7 @@ QString KrArcBaseManager::getShortTypeFromMime(const QString &mime)
// Reminder: If a mime type is added/removed/modified in that
// member function, it's important to research if the type has to
// be added/removed/modified in the `krarc.protocol` file, or
// in `KrServices::generateKrarcArchiveMimetypes()`
// in `KRarcHandler::KRarcHandler()`
// 7zip files are a not a normal case because their mimetype does not
// follow the norm of other types: zip, tar, lha, ace, arj, etc.
......
......@@ -39,8 +39,6 @@
#include "../krservices.h"
#include "../FileSystem/filesystemprovider.h"
extern KRarcHandler arcHandler;
AbstractThreadedJob::AbstractThreadedJob() : _maxProgressValue(0),
_currentProgress(0), _exiting(false), _jobThread(nullptr)
{
......@@ -650,7 +648,7 @@ bool AbstractJobThread::getArchiveInformation(QString &path, QString &type, QStr
QMimeType mt = db.mimeTypeForUrl(url);
QString mime = mt.isValid() ? mt.name() : QString();
bool encrypted = false;
type = arcHandler.getType(encrypted, path, mime);
type = krArcMan.getType(encrypted, path, mime);
// Check that the archive is supported
if (!KRarcHandler::arcSupported(type)) {
......
......@@ -32,6 +32,7 @@
#include <KConfigCore/KSharedConfig>
#include <KI18n/KLocalizedString>
#include <KIO/Global>
#include <KIOCore/KProtocolManager>
#include <KWallet/KWallet>
#include <KWidgetsAddons/KMessageBox>
#include <KWidgetsAddons/KPasswordDialog>
......@@ -84,8 +85,49 @@ public:
static QStringList arcProtocols = QString("tar;bzip;bzip2;lzma;xz;gzip;krarc;zip").split(';');
QMap<QString, QString>* KRarcHandler::slaveMap = nullptr;
KWallet::Wallet * KRarcHandler::wallet = nullptr;
KRarcHandler::KRarcHandler(QObject *parent) : QObject(parent)
{
// Reminder: If a mime type is added/removed/modified in that
// member function, it's important to research if the type has to
// be added/removed/modified in the `krarc.protocol` file, or
// in `KrArcBaseManager::getShortTypeFromMime(const QString &mime)`
// Hard-code these proven mimetypes openable by krarc protocol.
// They cannot be listed in krarc.protocol itself
// because it would baffle other file managers (like Dolphin).
krarcArchiveMimetypes = { QStringLiteral("application/x-deb"),
QStringLiteral("application/x-debian-package"),
QStringLiteral("application/vnd.debian.binary-package"),
QStringLiteral("application/x-java-archive"),
QStringLiteral("application/x-rpm"),
QStringLiteral("application/x-source-rpm"),
QStringLiteral("application/vnd.oasis.opendocument.chart"),
QStringLiteral("application/vnd.oasis.opendocument.database"),
QStringLiteral("application/vnd.oasis.opendocument.formula"),
QStringLiteral("application/vnd.oasis.opendocument.graphics"),
QStringLiteral("application/vnd.oasis.opendocument.presentation"),
QStringLiteral("application/vnd.oasis.opendocument.spreadsheet"),
QStringLiteral("application/vnd.oasis.opendocument.text"),
QStringLiteral("application/vnd.openxmlformats-officedocument.presentationml.presentation"),
QStringLiteral("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),
QStringLiteral("application/vnd.openxmlformats-officedocument.wordprocessingml.document"),
QStringLiteral("application/x-cbz"),
QStringLiteral("application/vnd.comicbook+zip"),
QStringLiteral("application/x-cbr"),
QStringLiteral("application/vnd.comicbook-rar"),
QStringLiteral("application/epub+zip"),
QStringLiteral("application/x-webarchive"),
QStringLiteral("application/x-plasma"),
QStringLiteral("application/vnd.rar") };
#ifdef KRARC_QUERY_ENABLED
krarcArchiveMimetypes += QSet<QString>::fromList(KProtocolInfo::archiveMimetypes("krarc"));
#endif
}
QStringList KRarcHandler::supportedPackers()
{
QStringList packers;
......@@ -670,3 +712,33 @@ void KRarcHandler::checkIf7zIsEncrypted(bool &encrypted, QString fileName)
encrypted = proc.isEncrypted();
}
QString KRarcHandler::registeredProtocol(const QString& mimetype)
{
if (slaveMap == nullptr) {
slaveMap = new QMap<QString, QString>();
KConfigGroup group(krConfig, "Protocols");
QStringList protList = group.readEntry("Handled Protocols", QStringList());
for (auto & it : protList) {
QStringList mimes = group.readEntry(QString("Mimes For %1").arg(it), QStringList());
for (auto & mime : mimes)
(*slaveMap)[mime] = it;
}
}
QString protocol = (*slaveMap)[mimetype];
if (protocol.isEmpty()) {
if (krarcArchiveMimetypes.contains(mimetype)) {
return QStringLiteral("krarc");
}
protocol = KProtocolManager::protocolForArchiveMimetype(mimetype);
}
return protocol;
}
void KRarcHandler::clearProtocolCache()
{
if (slaveMap)
delete slaveMap;
slaveMap = nullptr;
}
......@@ -24,6 +24,7 @@
// QtCore
#include <QStringList>
#include <QObject>
#include <QSet>
#include <QUrl>
#include <KCoreAddons/KProcess>
......@@ -55,6 +56,8 @@ class KRarcHandler: public QObject, public KrArcBaseManager
{
Q_OBJECT
public:
explicit KRarcHandler(QObject *parent = nullptr);
// return the number of files in the archive
static long arcFileCount(const QString& archive, const QString& type, const QString& password, KRarcObserver *observer);
// unpack an archive to destination directory
......@@ -75,12 +78,23 @@ public:
static QString getPassword(const QString& path);
// detects the archive type
void checkIf7zIsEncrypted(bool &, QString) override;
// returns the registered protocol associated with the mimetype
QString registeredProtocol(const QString& mimetype);
// clear the cache of handled protocols
static void clearProtocolCache();
private:
//! checks if a returned status ("exit code") of an archiving-related process is OK
static bool checkStatus(const QString& type, int exitCode);
static bool openWallet();
//! the list of archive mimetypes that are openable by the krarc protocol
QSet<QString> krarcArchiveMimetypes;
//! the cache of handled protocols
static QMap<QString, QString>* slaveMap;
static KWallet::Wallet * wallet;
};
......
......@@ -29,8 +29,6 @@
#include <KI18n/KLocalizedString>
extern KRarcHandler arcHandler;
PackJob::PackJob(const QUrl &srcUrl, const QUrl &destUrl, const QStringList & fileNames, const QString &type, const QMap<QString, QString> &packProps)
{
startAbstractJobThread(new PackThread(srcUrl, destUrl, fileNames, type, packProps));
......
......@@ -22,6 +22,7 @@
#include "../krglobal.h"
#include "../icon.h"
#include "../krservices.h"
#include "../Archive/krarchandler.h"
// QtCore
#include <QMimeDatabase>
......@@ -378,7 +379,7 @@ bool KgProtocols::apply()
group.writeEntry("Handled Protocols", protocolList);
krConfig->sync();
KrServices::clearProtocolCache();
KRarcHandler::clearProtocolCache();
emit sigChanged();
return false;
......
......@@ -913,7 +913,7 @@ QUrl ListPanelFunc::browsableArchivePath(const QString &filename)
QString mime = fileitem->getMime();
if(url.isLocalFile()) {
QString protocol = KrServices::registeredProtocol(mime);
QString protocol = krArcMan.registeredProtocol(mime);
if(!protocol.isEmpty()) {
url.setScheme(protocol);
return url;
......
......@@ -32,6 +32,7 @@
#include <KIO/Global>
#include "../krglobal.h"
#include "../Archive/krarchandler.h"
#include "../FileSystem/defaultfilesystem.h"
#include "../FileSystem/fileitem.h"
......@@ -43,8 +44,6 @@
static const QStringList TAR_TYPES = QStringList() << "tbz" << "tgz" << "tarz" << "tar" << "tlz";
extern KRarcHandler arcHandler;
KRSearchMod::KRSearchMod(const KRQuery *query)
: m_defaultFileSystem(nullptr), m_virtualFileSystem(nullptr), m_stopSearch(false)
{
......@@ -133,7 +132,7 @@ void KRSearchMod::scanDirectory(const QUrl &url)
// query search in archive; NOTE: only supported for local files
QUrl archiveURL = fileUrl;
bool encrypted;
const QString type = arcHandler.getType(encrypted, fileUrl.path(), fileItem->getMime());
const QString type = krArcMan.getType(encrypted, fileUrl.path(), fileItem->getMime());
if (!encrypted) {
archiveURL.setScheme(TAR_TYPES.contains(type) ? "tar" : "krarc");
......
......@@ -31,6 +31,7 @@
KConfig *KrGlobal::config = nullptr;
KMountMan *KrGlobal::mountMan = nullptr;
KRarcHandler *KrGlobal::arcMan = nullptr;
KrBookmarkHandler *KrGlobal::bookman = nullptr;
KRslots *KrGlobal::slot = nullptr;
KrusaderView *KrGlobal::mainView = nullptr;
......
......@@ -29,6 +29,7 @@
class KConfig;
class KMountMan;
class KRarcHandler;
class KrBookmarkHandler;
class KRslots;
class KrusaderView;
......@@ -44,6 +45,7 @@ class KrGlobal
public:
static KConfig *config; // allow everyone to access the config
static KMountMan *mountMan; // krusader's Mount Manager
static KRarcHandler *arcMan; //! Manages archives in several parts of the code
static KrBookmarkHandler *bookman;
static KRslots *slot;
static KrusaderView *mainView; // The GUI
......@@ -66,6 +68,7 @@ public:
#define krConfig KrGlobal::config
#define krMtMan (*(KrGlobal::mountMan))
#define krArcMan (*(KrGlobal::arcMan))
#define krBookMan KrGlobal::bookman
#define SLOTS KrGlobal::slot
#define MAIN_VIEW KrGlobal::mainView
......
......@@ -34,59 +34,8 @@
#include "krglobal.h"
#include "defaults.h"
QMap<QString, QString>* KrServices::slaveMap = nullptr;
QSet<QString> KrServices::krarcArchiveMimetypes = KrServices::generateKrarcArchiveMimetypes();
#ifdef KRARC_QUERY_ENABLED
QSet<QString> KrServices::isoArchiveMimetypes = QSet<QString>::fromList(KProtocolInfo::archiveMimetypes("iso"));
#else
QSet<QString> KrServices::isoArchiveMimetypes;
#endif
QString KrServices::GLOBAL_MESSAGE_PATTERN = "%{time hh:mm:ss.zzz}-%{type} %{category} %{function}@%{line} # %{message}";
QSet<QString> KrServices::generateKrarcArchiveMimetypes()
{
// Reminder: If a mime type is added/removed/modified in that
// member function, it's important to research if the type has to
// be added/removed/modified in the `krarc.protocol` file, or
// in `KrArcBaseManager::getShortTypeFromMime(const QString &mime)`
// Hard-code these proven mimetypes openable by krarc protocol.
// They cannot be listed in krarc.protocol itself
// because it would baffle other file managers (like Dolphin).
QSet<QString> mimes;
mimes += QString("application/x-deb");
mimes += QString("application/x-debian-package");
mimes += QString("application/vnd.debian.binary-package");
mimes += QString("application/x-java-archive");
mimes += QString("application/x-rpm");
mimes += QString("application/x-source-rpm");
mimes += QString("application/vnd.oasis.opendocument.chart");
mimes += QString("application/vnd.oasis.opendocument.database");
mimes += QString("application/vnd.oasis.opendocument.formula");
mimes += QString("application/vnd.oasis.opendocument.graphics");
mimes += QString("application/vnd.oasis.opendocument.presentation");
mimes += QString("application/vnd.oasis.opendocument.spreadsheet");
mimes += QString("application/vnd.oasis.opendocument.text");
mimes += QString("application/vnd.openxmlformats-officedocument.presentationml.presentation");
mimes += QString("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
mimes += QString("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
mimes += QString("application/x-cbz");
mimes += QString("application/vnd.comicbook+zip");
mimes += QString("application/x-cbr");
mimes += QString("application/vnd.comicbook-rar");
mimes += QString("application/epub+zip");
mimes += QString("application/x-webarchive");
mimes += QString("application/x-plasma");
mimes += QString("application/vnd.rar");
#ifdef KRARC_QUERY_ENABLED
mimes += QSet<QString>::fromList(KProtocolInfo::archiveMimetypes("krarc"));
#endif
return mimes;
}
bool KrServices::cmdExist(const QString& cmdName)
{
// Reminder: If that function is modified, it's important to research if the
......@@ -140,39 +89,13 @@ bool KrServices::isExecutable(const QString &path)
return info.isFile() && info.isExecutable();
}
QString KrServices::registeredProtocol(const QString& mimetype)
{
if (slaveMap == nullptr) {
slaveMap = new QMap<QString, QString>();
KConfigGroup group(krConfig, "Protocols");
QStringList protList = group.readEntry("Handled Protocols", QStringList());
for (auto & it : protList) {
QStringList mimes = group.readEntry(QString("Mimes For %1").arg(it), QStringList());
for (auto & mime : mimes)
(*slaveMap)[mime] = it;
}
}
QString protocol = (*slaveMap)[mimetype];
if (protocol.isEmpty()) {
if (krarcArchiveMimetypes.contains(mimetype)) {
return "krarc";
}
protocol = KProtocolManager::protocolForArchiveMimetype(mimetype);
}
return protocol;
}
bool KrServices::isoSupported(const QString& mimetype)
{
return isoArchiveMimetypes.contains(mimetype);
}
void KrServices::clearProtocolCache()
{
if (slaveMap)
delete slaveMap;
slaveMap = nullptr;
#ifdef KRARC_QUERY_ENABLED
return KProtocolInfo::archiveMimetypes("iso").contains(mimetype);
#else
return false;
#endif
}
bool KrServices::fileToStringList(QTextStream *stream, QStringList& target, bool keepEmptyLines)
......
......@@ -41,10 +41,8 @@ public:
static QString chooseFullPathName(QStringList names, const QString& confName);
static QString fullPathName(const QString& name, QString confName = QString());
static bool isExecutable(const QString &path);
static QString registeredProtocol(const QString& mimetype);
static bool isoSupported(const QString& mimetype);
static QString urlToLocalPath(const QUrl &url);
static void clearProtocolCache();
static bool fileToStringList(QTextStream *stream, QStringList& target, bool keepEmptyLines = false);
static bool fileToStringList(QFile *file, QStringList& target, bool keepEmptyLines = false);
static QString quote(const QString& name);
......@@ -67,10 +65,6 @@ protected:
private:
KrServices() {}
~KrServices() {}
static QSet<QString> generateKrarcArchiveMimetypes();
static QMap<QString, QString>* slaveMap;
static QSet<QString> krarcArchiveMimetypes;
static QSet<QString> isoArchiveMimetypes;
static void krMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg);
};
......
......@@ -54,6 +54,7 @@
#include "defaults.h"
#include "kractions.h"
#include "krarchandler.h"
#include "krglobal.h"
#include "krservices.h"
#include "krslots.h"
......@@ -136,6 +137,9 @@ Krusader::Krusader(const QCommandLineParser &parser) : KParts::MainWindow(nullpt
KMessageBox::error(krApp, message);
}
// create an object that manages archives in several parts of the source code
KrGlobal::arcMan = new KRarcHandler(this);
// create MountMan
KrGlobal::mountMan = new KMountMan(this);
connect(KrGlobal::mountMan, &KMountMan::refreshPanel, SLOTS, &KRslots::refresh);
......
......@@ -87,9 +87,6 @@ void openTabsRemote(QStringList tabs, bool left, const QString& appName)
fprintf(stderr, "DBus Error: %s, %s\n", reply.error().name().toLocal8Bit().constData(), reply.error().message().toLocal8Bit().constData());
}
//! An object that manages archives in several parts of the source code.
KRarcHandler arcHandler;
int main(int argc, char *argv[])
{
// set global log message format
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment