File indexing completed on 2024-04-28 04:58:12

0001 /* This file is part of the KDE project
0002     SPDX-FileCopyrightText: 2023 Stefano Crocco <stefano.crocco@alice.it>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "libkonq_utils.h"
0008 
0009 #include <KBookmarkManager>
0010 #include <QStandardPaths>
0011 
0012 #include <QFileDialog>
0013 
0014 using namespace Konq;
0015 
0016 KBookmarkManager * Konq::userBookmarksManager()
0017 {
0018 #if QT_VERSION_MAJOR < 6
0019     return KBookmarkManager::userBookmarksManager();
0020 #else
0021     static const QString bookmarksFile = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/konqueror/bookmarks.xml");
0022     static KBookmarkManager s_manager(bookmarksFile);
0023     return &s_manager;
0024 #endif
0025 }
0026 
0027 //Code copied from kparts/browserrun.cpp (KF5.109) written by David Faure <faure@kde.org>
0028 QUrl Konq::makeErrorUrl(int error, const QString& errorText, const QUrl& initialUrl)
0029 {
0030     /*
0031      * The format of the error:/ URL is error:/?query#url,
0032      * where two variables are passed in the query:
0033      * error = int kio error code, errText = QString error text from kio
0034      * The sub-url is the URL that we were trying to open.
0035      */
0036     QUrl newURL(QStringLiteral("error:/?error=%1&errText=%2").arg(error).arg(QString::fromUtf8(QUrl::toPercentEncoding(errorText))));
0037 
0038     QString cleanedOrigUrl = initialUrl.toString();
0039     QUrl runURL(cleanedOrigUrl);
0040     if (runURL.isValid()) {
0041         runURL.setPassword(QString()); // don't put the password in the error URL
0042         cleanedOrigUrl = runURL.toString();
0043     }
0044 
0045     newURL.setFragment(cleanedOrigUrl);
0046     return newURL;
0047 }
0048 
0049 QString Konq::askDownloadLocation(const QString& suggestedFileName, QWidget* parent, const QString& startingDir)
0050 {
0051     QFileDialog dlg(parent);
0052     dlg.setAcceptMode(QFileDialog::AcceptSave);
0053     dlg.setOption(QFileDialog::DontConfirmOverwrite, false);
0054     dlg.selectFile(suggestedFileName);
0055     dlg.setDirectory(!startingDir.isEmpty() ? startingDir : QStandardPaths::writableLocation(QStandardPaths::DownloadLocation));
0056 
0057     if (dlg.exec() == QDialog::Rejected) {
0058         return QString();
0059     }
0060     return dlg.selectedUrls().first().path();
0061 }