Changeset View
Changeset View
Standalone View
Standalone View
src/opencomposerjob.cpp
Show All 13 Lines | |||||
14 | * You should have received a copy of the GNU Library General Public License | 14 | * You should have received a copy of the GNU Library General Public License | ||
15 | * along with this library; see the file COPYING.LIB. If not, write to the | 15 | * along with this library; see the file COPYING.LIB. If not, write to the | ||
16 | * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | 16 | * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
17 | * 02110-1301, USA. | 17 | * 02110-1301, USA. | ||
18 | */ | 18 | */ | ||
19 | 19 | | |||
20 | #include "opencomposerjob.h" | 20 | #include "opencomposerjob.h" | ||
21 | 21 | | |||
22 | #include <KDBusServiceStarter> | | |||
23 | #include <KLocalizedString> | 22 | #include <KLocalizedString> | ||
24 | 23 | | |||
25 | #include <QDBusConnectionInterface> | 24 | #include <QDBusConnectionInterface> | ||
26 | #include <QDBusInterface> | 25 | #include <QDBusInterface> | ||
27 | #include <QProcess> | 26 | #include <QProcess> | ||
28 | #include <QTimer> | 27 | #include <QTimer> | ||
29 | 28 | | |||
30 | using namespace IncidenceEditorNG; | 29 | using namespace IncidenceEditorNG; | ||
31 | 30 | | |||
32 | OpenComposerJob::OpenComposerJob(QObject *parent, const QString &to, const QString &cc, const QString &bcc, const KMime::Message::Ptr &message, const KIdentityManagement::Identity &identity) | 31 | OpenComposerJob::OpenComposerJob(QObject *parent, const QString &to, const QString &cc, const QString &bcc, const KMime::Message::Ptr &message, const KIdentityManagement::Identity &identity) | ||
33 | : KJob(parent) | 32 | : KJob(parent) | ||
34 | , mTo(to) | 33 | , mTo(to) | ||
35 | , mCc(cc) | 34 | , mCc(cc) | ||
36 | , mBcc(bcc) | 35 | , mBcc(bcc) | ||
37 | , mMessage(message) | 36 | , mMessage(message) | ||
38 | , mIdentity(identity) | 37 | , mIdentity(identity) | ||
39 | , mSuccess(false) | | |||
40 | { | 38 | { | ||
41 | connect( | | |||
42 | QDBusConnection::sessionBus().interface(), &QDBusConnectionInterface::serviceOwnerChanged, | | |||
43 | this, &OpenComposerJob::slotServiceOwnerChanged); | | |||
44 | } | 39 | } | ||
45 | 40 | | |||
46 | OpenComposerJob::~OpenComposerJob() | 41 | OpenComposerJob::~OpenComposerJob() | ||
47 | { | 42 | { | ||
48 | } | 43 | } | ||
49 | 44 | | |||
50 | void OpenComposerJob::start() | 45 | void OpenComposerJob::start() | ||
51 | { | 46 | { | ||
52 | mSuccess = false; | | |||
53 | if (QDBusConnection::sessionBus().interface()->isServiceRegistered(QStringLiteral( | | |||
54 | "org.kde.kmail"))) { | | |||
55 | QMetaObject::invokeMethod(this, &OpenComposerJob::processMail, Qt::QueuedConnection); | | |||
56 | } | | |||
57 | //Check if Kontact is already running and if not ... | | |||
58 | int result = KDBusServiceStarter::self()->findServiceFor(QStringLiteral("DBUS/Mailer"), | | |||
59 | QString(), | | |||
60 | &mError, &mDBusService); | | |||
61 | if (result != 0) { | | |||
62 | // ... start Kontact | | |||
63 | result = KDBusServiceStarter::self()->startServiceFor(QStringLiteral( | | |||
64 | "DBUS/Mailer"), QString(), | | |||
65 | &mError, &mDBusService); | | |||
66 | if (result != 0) { | | |||
67 | const bool ok = QProcess::startDetached(QStringLiteral("kontact")); | | |||
68 | if (!ok) { | | |||
69 | setError(KJob::UserDefinedError); | | |||
70 | setErrorText(i18nc("errormessage: can't create a kontact process", | | |||
71 | "Can't create composer: Failed to start kontact.")); | | |||
72 | emitResult(); | | |||
73 | return; | | |||
74 | } | | |||
75 | } | | |||
76 | } | | |||
77 | | ||||
78 | QTimer::singleShot(10000, this, &OpenComposerJob::timeout); | | |||
79 | } | | |||
80 | | ||||
81 | void OpenComposerJob::timeout() | | |||
82 | { | | |||
83 | if (!mSuccess) { | | |||
84 | setError(KJob::UserDefinedError); | | |||
85 | setErrorText(i18nc( | | |||
86 | "errormessage: No connection via dbus nor starting kontact process worked.", | | |||
87 | "Can't create composer: Neither dbus nor kontact responded in time.")); | | |||
88 | emitResult(); | | |||
89 | } | | |||
90 | } | | |||
91 | | ||||
92 | void OpenComposerJob::slotServiceOwnerChanged(const QString &name, const QString &oldOwner, const QString &newOwner) | | |||
93 | { | | |||
94 | Q_UNUSED(oldOwner); | | |||
95 | if (name == QLatin1String("org.kde.kmail") && !newOwner.isEmpty()) { | | |||
96 | processMail(); | | |||
97 | } | | |||
98 | } | | |||
99 | | ||||
100 | void OpenComposerJob::processMail() | | |||
101 | { | | |||
102 | Q_ASSERT(mMessage); | 47 | Q_ASSERT(mMessage); | ||
103 | mSuccess = true; | | |||
104 | 48 | | |||
105 | unsigned int identity = mIdentity.uoid(); | 49 | unsigned int identity = mIdentity.uoid(); | ||
106 | 50 | | |||
107 | QString subject = mMessage->subject()->asUnicodeString(); | 51 | QString subject = mMessage->subject()->asUnicodeString(); | ||
108 | QString body = QString::fromUtf8(mMessage->contents()[0]->body()); | 52 | QString body = QString::fromUtf8(mMessage->contents()[0]->body()); | ||
109 | 53 | | |||
110 | QList<QVariant> messages; | 54 | QList<QVariant> messages; | ||
111 | 55 | | |||
Show All 20 Lines | 66 | } else { | |||
132 | QString attachParamValue = attachment->contentType()->parameter(QStringLiteral("method")); | 76 | QString attachParamValue = attachment->contentType()->parameter(QStringLiteral("method")); | ||
133 | QByteArray attachData = attachment->encodedBody(); | 77 | QByteArray attachData = attachment->encodedBody(); | ||
134 | 78 | | |||
135 | messages << mTo << mCc << mBcc << subject << body | 79 | messages << mTo << mCc << mBcc << subject << body | ||
136 | << attachName << attachCte << attachData << attachType << attachSubType | 80 | << attachName << attachCte << attachData << attachType << attachSubType | ||
137 | << attachParamAttr << attachParamValue << attachContDisp << attachCharset | 81 | << attachParamAttr << attachParamValue << attachContDisp << attachCharset | ||
138 | << identity; | 82 | << identity; | ||
139 | } | 83 | } | ||
84 | | ||||
85 | // with D-Bus autostart, this will start kmail if it's not running yet | ||||
140 | QDBusInterface kmailObj(QStringLiteral("org.kde.kmail"), QStringLiteral( | 86 | QDBusInterface kmailObj(QStringLiteral("org.kde.kmail"), QStringLiteral( | ||
141 | "/KMail"), QStringLiteral("org.kde.kmail.kmail")); | 87 | "/KMail"), QStringLiteral("org.kde.kmail.kmail")); | ||
142 | 88 | | |||
143 | QDBusReply<int> composerDbusPath = kmailObj.callWithArgumentList(QDBus::AutoDetect, QStringLiteral( | 89 | QDBusReply<int> composerDbusPath = kmailObj.callWithArgumentList(QDBus::AutoDetect, QStringLiteral( | ||
144 | "openComposer"), messages); | 90 | "openComposer"), messages); | ||
145 | 91 | | |||
146 | if (!composerDbusPath.isValid()) { | 92 | if (!composerDbusPath.isValid()) { | ||
147 | setError(KJob::UserDefinedError); | 93 | setError(KJob::UserDefinedError); | ||
148 | setErrorText(i18nc("errormessage: dbus is running but still no connection kmail", | 94 | setErrorText(i18nc("errormessage: dbus is running but still no connection kmail", | ||
149 | "Cannot connect to email service")); | 95 | "Cannot connect to email service")); | ||
150 | } | 96 | } | ||
151 | emitResult(); | 97 | emitResult(); | ||
152 | } | 98 | } |