Pimcommon

pimutil.cpp
1/*******************************************************************************
2**
3** SPDX-FileCopyrightText: 2005 Till Adam <adam@kde.org>
4**
5** SPDX-License-Identifier: GPL-2.0-or-later
6**
7*******************************************************************************/
8
9#include "pimutil.h"
10using namespace Qt::Literals::StringLiterals;
11
12#include "imapresourcesettings.h"
13
14#include <KEmailAddress>
15#include <KLocalizedString>
16#include <KMessageBox>
17#include <QDesktopServices>
18#include <QFileDialog>
19
20#include <QCoreApplication>
21#include <QPointer>
22#include <QStandardPaths>
23#include <QTextStream>
24#include <QUrlQuery>
25#include <QWidget>
26
27#include <cerrno>
28
29OrgKdeAkonadiImapSettingsInterface *PimCommon::Util::createImapSettingsInterface(const QString &ident)
30{
31 if (isImapResource(ident)) {
32 return new OrgKdeAkonadiImapSettingsInterface("org.freedesktop.Akonadi.Resource."_L1 + ident,
33 QStringLiteral("/Settings"),
35 } else {
36 return nullptr;
37 }
38}
39
40void PimCommon::Util::saveTextAs(const QString &text, const QString &filter, QWidget *parent, const QUrl &url, const QString &caption)
41{
42 QPointer<QFileDialog> fdlg(new QFileDialog(parent, QString(), url.path(), filter));
43 if (!caption.isEmpty()) {
44 fdlg->setWindowTitle(caption);
45 }
46 fdlg->setAcceptMode(QFileDialog::AcceptSave);
47 if (fdlg->exec() == QDialog::Accepted) {
48 const QString fileName = fdlg->selectedFiles().at(0);
49 if (!saveToFile(fileName, text)) {
50 KMessageBox::error(parent,
51 i18n("Could not write the file %1:\n"
52 "\"%2\" is the detailed error description.",
53 fileName,
54 QString::fromLocal8Bit(strerror(errno))),
55 i18nc("@title:window", "Save File Error"));
56 }
57 }
58 delete fdlg;
59}
60
61bool PimCommon::Util::saveToFile(const QString &filename, const QString &text)
62{
63 QFile file(filename);
64 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
65 return false;
66 }
67 QTextStream out(&file);
68 out << text;
69 file.close();
70 return true;
71}
72
73QString PimCommon::Util::loadToFile(const QString &filter, QWidget *parent, const QUrl &url, const QString &caption)
74{
75 QPointer<QFileDialog> fdlg(new QFileDialog(parent, QString(), url.path(), filter));
76 if (!caption.isEmpty()) {
77 fdlg->setWindowTitle(caption);
78 }
79 fdlg->setAcceptDrops(QFileDialog::AcceptOpen);
80 QString result;
81 if (fdlg->exec() == QDialog::Accepted) {
82 const QString fileName = fdlg->selectedFiles().at(0);
83 QFile file(fileName);
84 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
85 KMessageBox::error(parent,
86 i18n("Could not read the file %1:\n"
87 "\"%2\" is the detailed error description.",
88 fileName,
89 QString::fromLocal8Bit(strerror(errno))),
90 i18nc("@title:window", "Load File Error"));
91 } else {
92 result = QString::fromUtf8(file.readAll());
93 file.close();
94 }
95 }
96 delete fdlg;
97 return result;
98}
99
100bool PimCommon::Util::isImapResource(const QString &identifier)
101{
102 return identifier.startsWith(KOLAB_RESOURCE_IDENTIFIER) || identifier.startsWith(IMAP_RESOURCE_IDENTIFIER)
103 || identifier.startsWith(GMAIL_RESOURCE_IDENTIFIER);
104}
105
106QUrl PimCommon::Util::generateHelpUrl(const QString &docfile, const QString &anchor)
107{
108 QUrl generatedUrl;
109 if (!docfile.isEmpty()) {
110 QUrl url;
111 url = QUrl(QStringLiteral("help:/")).resolved(QUrl(docfile));
112 if (!anchor.isEmpty()) {
113 QUrlQuery query(url);
114 query.addQueryItem(QStringLiteral("anchor"), anchor);
115 url.setQuery(query);
116 }
117 generatedUrl = std::move(url);
118 }
119 return generatedUrl;
120}
121
122void PimCommon::Util::invokeHelp(const QString &docfile, const QString &anchor)
123{
124 const QUrl url = generateHelpUrl(docfile, anchor);
125 if (!url.isEmpty()) {
126 // launch khelpcenter, or a browser for URIs not handled by khelpcenter
128 }
129}
130
131QStringList PimCommon::Util::generateEmailList(const QStringList &list)
132{
133 QString str;
134 const auto numberOfElement{list.count()};
135 for (int i = 0; i < numberOfElement; i++) {
136 QString tmpStr = list.at(i);
137 if (!tmpStr.trimmed().isEmpty()) {
138 if (!str.isEmpty()) {
139 str.append(", "_L1);
140 }
141 str.append(tmpStr);
142 }
143 }
145 return emails;
146}
147
148QString PimCommon::Util::findExecutable(const QString &exec)
149{
150#ifdef Q_OS_WIN
151 const QString executableName = exec + QStringLiteral(".exe");
153 if (path.isEmpty()) {
154 path = QStandardPaths::findExecutable(executableName);
155 }
156#else
158#endif
159 return path;
160}
KCODECS_EXPORT QStringList splitAddressList(const QString &aStr)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
QString path(const QString &relativePath)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
QString applicationDirPath()
QDBusConnection sessionBus()
bool openUrl(const QUrl &url)
const_reference at(qsizetype i) const const
qsizetype count() const const
QString findExecutable(const QString &executableName, const QStringList &paths)
QString & append(QChar ch)
const QChar at(qsizetype position) const const
QString fromLocal8Bit(QByteArrayView str)
QString fromUtf8(QByteArrayView str)
bool isEmpty() const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QString trimmed() const const
bool isEmpty() const const
QString path(ComponentFormattingOptions options) const const
QUrl resolved(const QUrl &relative) const const
void setQuery(const QString &query, ParsingMode mode)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:12:05 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.