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 <QPointer>
21#include <QTextStream>
22#include <QUrlQuery>
23#include <QWidget>
24
25#include <cerrno>
26
27OrgKdeAkonadiImapSettingsInterface *PimCommon::Util::createImapSettingsInterface(const QString &ident)
28{
29 if (isImapResource(ident)) {
30 return new OrgKdeAkonadiImapSettingsInterface("org.freedesktop.Akonadi.Resource."_L1 + ident,
31 QStringLiteral("/Settings"),
33 } else {
34 return nullptr;
35 }
36}
37
38void PimCommon::Util::saveTextAs(const QString &text, const QString &filter, QWidget *parent, const QUrl &url, const QString &caption)
39{
40 QPointer<QFileDialog> fdlg(new QFileDialog(parent, QString(), url.path(), filter));
41 if (!caption.isEmpty()) {
42 fdlg->setWindowTitle(caption);
43 }
44 fdlg->setAcceptMode(QFileDialog::AcceptSave);
45 if (fdlg->exec() == QDialog::Accepted) {
46 const QString fileName = fdlg->selectedFiles().at(0);
47 if (!saveToFile(fileName, text)) {
48 KMessageBox::error(parent,
49 i18n("Could not write the file %1:\n"
50 "\"%2\" is the detailed error description.",
51 fileName,
52 QString::fromLocal8Bit(strerror(errno))),
53 i18nc("@title:window", "Save File Error"));
54 }
55 }
56 delete fdlg;
57}
58
59bool PimCommon::Util::saveToFile(const QString &filename, const QString &text)
60{
61 QFile file(filename);
62 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
63 return false;
64 }
65 QTextStream out(&file);
66 out << text;
67 file.close();
68 return true;
69}
70
71QString PimCommon::Util::loadToFile(const QString &filter, QWidget *parent, const QUrl &url, const QString &caption)
72{
73 QPointer<QFileDialog> fdlg(new QFileDialog(parent, QString(), url.path(), filter));
74 if (!caption.isEmpty()) {
75 fdlg->setWindowTitle(caption);
76 }
77 fdlg->setAcceptDrops(QFileDialog::AcceptOpen);
78 QString result;
79 if (fdlg->exec() == QDialog::Accepted) {
80 const QString fileName = fdlg->selectedFiles().at(0);
81 QFile file(fileName);
82 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
83 KMessageBox::error(parent,
84 i18n("Could not read the file %1:\n"
85 "\"%2\" is the detailed error description.",
86 fileName,
87 QString::fromLocal8Bit(strerror(errno))),
88 i18nc("@title:window", "Load File Error"));
89 } else {
90 result = QString::fromUtf8(file.readAll());
91 file.close();
92 }
93 }
94 delete fdlg;
95 return result;
96}
97
98bool PimCommon::Util::isImapResource(const QString &identifier)
99{
100 return identifier.startsWith(KOLAB_RESOURCE_IDENTIFIER) || identifier.startsWith(IMAP_RESOURCE_IDENTIFIER)
101 || identifier.startsWith(GMAIL_RESOURCE_IDENTIFIER);
102}
103
104QUrl PimCommon::Util::generateHelpUrl(const QString &docfile, const QString &anchor)
105{
106 QUrl generatedUrl;
107 if (!docfile.isEmpty()) {
108 QUrl url;
109 url = QUrl(QStringLiteral("help:/")).resolved(QUrl(docfile));
110 if (!anchor.isEmpty()) {
111 QUrlQuery query(url);
112 query.addQueryItem(QStringLiteral("anchor"), anchor);
113 url.setQuery(query);
114 }
115 generatedUrl = std::move(url);
116 }
117 return generatedUrl;
118}
119
120void PimCommon::Util::invokeHelp(const QString &docfile, const QString &anchor)
121{
122 const QUrl url = generateHelpUrl(docfile, anchor);
123 if (!url.isEmpty()) {
124 // launch khelpcenter, or a browser for URIs not handled by khelpcenter
126 }
127}
128
129QStringList PimCommon::Util::generateEmailList(const QStringList &list)
130{
131 QString str;
132 const auto numberOfElement{list.count()};
133 for (int i = 0; i < numberOfElement; i++) {
134 QString tmpStr = list.at(i);
135 if (!tmpStr.trimmed().isEmpty()) {
136 if (!str.isEmpty()) {
137 str.append(", "_L1);
138 }
139 str.append(tmpStr);
140 }
141 }
143 return emails;
144}
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)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
QDBusConnection sessionBus()
bool openUrl(const QUrl &url)
const_reference at(qsizetype i) const const
qsizetype count() const const
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 Mon Nov 4 2024 16:39:22 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.