1 /***************************************************************************
2 This class provides notifications for Smb4K.
3 -------------------
4 begin : Son Jun 27 2010
5 copyright : (C) 2010-2019 by Alexander Reinholdt
6 email : alexander.reinholdt@kdemail.net
7 ***************************************************************************/
8
9 /***************************************************************************
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 * This program is distributed in the hope that it will be useful, but *
16 * WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
18 * General Public License for more details. *
19 * *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program; if not, write to the *
22 * Free Software Foundation, Inc., 51 Franklin Street, Suite 500, Boston,*
23 * MA 02110-1335, USA *
24 ***************************************************************************/
25
26 // application specific includes
27 #include "smb4knotification.h"
28 #include "smb4knotification_p.h"
29 #include "smb4ksettings.h"
30 #include "smb4kbookmark.h"
31 #include "smb4kworkgroup.h"
32 #include "smb4khost.h"
33 #include "smb4kshare.h"
34
35 // KDE includes
36 #define TRANSLATION_DOMAIN "smb4k-core"
37 #include <KAuth/KAuthActionReply>
38 #include <KNotifications/KNotification>
39 #include <KI18n/KLocalizedString>
40 #include <KIconThemes/KIconLoader>
41 #include "kiconthemes_version.h"
42
43 using namespace KAuth;
44
45 //
46 // Notifications
47 //
48
shareMounted(const SharePtr & share)49 void Smb4KNotification::shareMounted(const SharePtr &share)
50 {
51 Q_ASSERT(share);
52
53 if (share)
54 {
55 QUrl mountpoint = QUrl::fromLocalFile(share->path());
56
57 Smb4KNotifier *notification = new Smb4KNotifier("shareMounted");
58 notification->setText(i18n("<p>The share <b>%1</b> has been mounted to <b>%2</b>.</p>", share->displayString(), share->path()));
59 notification->setPixmap(KIconLoader::global()->loadIcon("folder-network", KIconLoader::NoGroup, 0, KIconLoader::DefaultState, QStringList("emblem-mounted")));
60 notification->setActions(QStringList(i18n("Open")));
61 notification->setMountpoint(mountpoint);
62 notification->sendEvent();
63 }
64 }
65
66
shareUnmounted(const SharePtr & share)67 void Smb4KNotification::shareUnmounted(const SharePtr &share)
68 {
69 Q_ASSERT(share);
70
71 if (share)
72 {
73 Smb4KNotifier *notification = new Smb4KNotifier("shareUnmounted");
74 notification->setText(i18n("<p>The share <b>%1</b> has been unmounted from <b>%2</b>.</p>", share->displayString(), share->path()));
75 notification->setPixmap(KIconLoader::global()->loadIcon("folder-network", KIconLoader::NoGroup, 0, KIconLoader::DefaultState, QStringList("emblem-unmounted")));
76 notification->sendEvent();
77 }
78 }
79
80
sharesMounted(int number)81 void Smb4KNotification::sharesMounted(int number)
82 {
83 Smb4KNotifier *notification = new Smb4KNotifier("sharesMounted");
84 notification->setText(i18np("<p>%1 share has been mounted.</p>", "<p>%1 shares have been mounted.</p>", number));
85 notification->setPixmap(KIconLoader::global()->loadIcon("folder-network", KIconLoader::NoGroup, 0, KIconLoader::DefaultState, QStringList("emblem-mounted")));
86 notification->sendEvent();
87 }
88
89
sharesUnmounted(int number)90 void Smb4KNotification::sharesUnmounted(int number)
91 {
92 Smb4KNotifier *notification = new Smb4KNotifier("sharesUnmounted");
93 notification->setText(i18np("<p>%1 share has been unmounted.</p>", "<p>%1 shares have been unmounted.</p>", number));
94 notification->setPixmap(KIconLoader::global()->loadIcon("folder-network", KIconLoader::NoGroup, 0, KIconLoader::DefaultState, QStringList("emblem-unmounted")));
95 notification->sendEvent();
96 }
97
98
99 //
100 // Warnings
101 //
102
openingWalletFailed(const QString & name)103 void Smb4KNotification::openingWalletFailed(const QString& name)
104 {
105 Smb4KNotifier *notification = new Smb4KNotifier("openingWalletFailed");
106 notification->setText(i18n("<p>Opening the wallet <b>%1</b> failed.</p>", name));
107 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-warning", KIconLoader::NoGroup, 0, KIconLoader::DefaultState));
108 notification->sendEvent();
109 }
110
111
credentialsNotAccessible()112 void Smb4KNotification::credentialsNotAccessible()
113 {
114 Smb4KNotifier *notification = new Smb4KNotifier("credentialsNotAccessible");
115 notification->setText(i18n("<p>The credentials stored in the wallet could not be accessed. "
116 "There is either no wallet available or it could not be opened.</p>"));
117 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-warning", KIconLoader::NoGroup, 0, KIconLoader::DefaultState));
118 notification->sendEvent();
119 }
120
121
mimetypeNotSupported(const QString & mimetype)122 void Smb4KNotification::mimetypeNotSupported(const QString& mimetype)
123 {
124 Smb4KNotifier *notification = new Smb4KNotifier("mimetypeNotSupported");
125 notification->setText(i18n("<p>The mimetype <b>%1</b> is not supported for printing. "
126 "Please convert the file to PDF or Postscript and try again.</p>", mimetype));
127 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-warning", KIconLoader::NoGroup, 0, KIconLoader::DefaultState));
128 notification->sendEvent();
129 }
130
131
bookmarkExists(Smb4KBookmark * bookmark)132 void Smb4KNotification::bookmarkExists(Smb4KBookmark* bookmark)
133 {
134 if (bookmark)
135 {
136 Smb4KNotifier *notification = new Smb4KNotifier("bookmarkExists");
137 notification->setText(i18n("<p>The bookmark for share <b>%1</b> already exists and will be skipped.</p>",
138 bookmark->displayString()));
139 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-warning", KIconLoader::NoGroup, 0, KIconLoader::DefaultState));
140 notification->sendEvent();
141 }
142 }
143
144
bookmarkLabelInUse(Smb4KBookmark * bookmark)145 void Smb4KNotification::bookmarkLabelInUse(Smb4KBookmark* bookmark)
146 {
147 if (bookmark)
148 {
149 Smb4KNotifier *notification = new Smb4KNotifier("bookmarkLabelInUse");
150 notification->setText(i18n("<p>The label <b>%1</b> of the bookmark for the share <b>%2</b> "
151 "is already being used and will automatically be renamed.</p>",
152 bookmark->label(), bookmark->displayString()));
153 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-warning", KIconLoader::NoGroup, 0, KIconLoader::DefaultState));
154 notification->sendEvent();
155 }
156 }
157
158
sambaConfigFileMissing()159 void Smb4KNotification::sambaConfigFileMissing()
160 {
161 Smb4KNotifier *notification = new Smb4KNotifier("sambaConfigFileMissing");
162 notification->setText(i18n("The configuration file for the Samba suite <b>smb.conf</b> is missing. This is not "
163 "a fatal error, but you should consider creating one."));
164 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-warning", KIconLoader::NoGroup, 0, KIconLoader::DefaultState));
165 notification->sendEvent();
166 }
167
168
169 //
170 // Errors
171 //
mountingFailed(const SharePtr & share,const QString & err_msg)172 void Smb4KNotification::mountingFailed(const SharePtr &share, const QString& err_msg)
173 {
174 if (share)
175 {
176 QString text;
177
178 if (!err_msg.isEmpty())
179 {
180 text = i18n("<p>Mounting the share <b>%1</b> failed:</p><p><tt>%2</tt></p>", share->displayString(), err_msg);
181 }
182 else
183 {
184 text = i18n("<p>Mounting the share <b>%1</b> failed.</p>", share->displayString());
185 }
186
187 Smb4KNotifier *notification = new Smb4KNotifier("mountingFailed");
188 notification->setText(text);
189 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-error", KIconLoader::NoGroup, 0, KIconLoader::DefaultState));
190 notification->sendEvent();
191 }
192 }
193
194
unmountingFailed(const SharePtr & share,const QString & err_msg)195 void Smb4KNotification::unmountingFailed(const SharePtr &share, const QString& err_msg)
196 {
197 if (share)
198 {
199 QString text;
200
201 if (!err_msg.isEmpty())
202 {
203 text = i18n("<p>Unmounting the share <b>%1</b> from <b>%2</b> failed:</p><p><tt>%3</tt></p>", share->displayString(), share->path(), err_msg);
204 }
205 else
206 {
207 text = i18n("<p>Unmounting the share <b>%1</b> from <b>%2</b> failed.</p>", share->displayString(), share->path());
208 }
209
210 Smb4KNotifier *notification = new Smb4KNotifier("unmountingFailed");
211 notification->setText(text);
212 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-error", KIconLoader::NoGroup, 0, KIconLoader::DefaultState));
213 notification->sendEvent();
214 }
215 }
216
217
unmountingNotAllowed(const SharePtr & share)218 void Smb4KNotification::unmountingNotAllowed(const SharePtr &share)
219 {
220 Q_ASSERT(share);
221
222 if (share)
223 {
224 Smb4KNotifier *notification = new Smb4KNotifier("unmountingNotAllowed");
225 notification->setText(i18n("<p>You are not allowed to unmount the share <b>%1</b> from <b>%2</b>. "
226 "It is owned by the user <b>%3</b>.</p>", share->displayString(), share->path(), share->user().loginName()));
227 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-error", KIconLoader::NoGroup, 0, KIconLoader::DefaultState));
228 notification->sendEvent();
229 }
230 }
231
232
synchronizationFailed(const QUrl & src,const QUrl & dest,const QString & err_msg)233 void Smb4KNotification::synchronizationFailed(const QUrl& src, const QUrl& dest, const QString& err_msg)
234 {
235 QString text;
236
237 if (!err_msg.isEmpty())
238 {
239 text = i18n("<p>Synchronizing <b>%1</b> with <b>%2</b> failed:</p><p><tt>%3</tt></p>", dest.path(), src.path(), err_msg);
240 }
241 else
242 {
243 text = i18n("<p>Synchronizing <b>%1</b> with <b>%2</b> failed.</p>", dest.path(), src.path());
244 }
245
246 Smb4KNotifier *notification = new Smb4KNotifier("synchronizationFailed");
247 notification->setText(text);
248 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-error", KIconLoader::NoGroup, 0, KIconLoader::DefaultState));
249 notification->sendEvent();
250 }
251
252
commandNotFound(const QString & command)253 void Smb4KNotification::commandNotFound(const QString& command)
254 {
255 Smb4KNotifier *notification = new Smb4KNotifier("commandNotFound");
256 notification->setText(i18n("<p>The command <b>%1</b> could not be found. Please check your installation.</p>", command));
257 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-error", KIconLoader::NoGroup, 0, KIconLoader::DefaultState));
258 notification->sendEvent();
259 }
260
261
cannotBookmarkPrinter(const SharePtr & share)262 void Smb4KNotification::cannotBookmarkPrinter(const SharePtr &share)
263 {
264 if (share && share->isPrinter())
265 {
266 Smb4KNotifier *notification = new Smb4KNotifier("cannotBookmarkPrinter");
267 notification->setText(i18n("<p>The share <b>%1</b> is a printer and cannot be bookmarked.</p>", share->displayString()));
268 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-error", KIconLoader::NoGroup, 0, KIconLoader::DefaultState));
269 notification->sendEvent();
270 }
271 }
272
273
fileNotFound(const QString & fileName)274 void Smb4KNotification::fileNotFound(const QString& fileName)
275 {
276 Smb4KNotifier *notification = new Smb4KNotifier("fileNotFound");
277 notification->setText(i18n("<p>The file <b>%1</b> could not be found.</p>", fileName));
278 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-error", KIconLoader::NoGroup, 0, KIconLoader::DefaultState));
279 notification->sendEvent();
280 }
281
282
openingFileFailed(const QFile & file)283 void Smb4KNotification::openingFileFailed(const QFile& file)
284 {
285 QString text;
286
287 if (!file.errorString().isEmpty())
288 {
289 text = i18n("<p>Opening the file <b>%1</b> failed:</p><p><tt>%2</tt></p>", file.fileName(), file.errorString());
290 }
291 else
292 {
293 text = i18n("<p>Opening the file <b>%1</b> failed.</p>", file.fileName());
294 }
295
296 Smb4KNotifier *notification = new Smb4KNotifier("openingFileFailed");
297 notification->setText(text);
298 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-error", KIconLoader::NoGroup, 0, KIconLoader::DefaultState));
299 notification->sendEvent();
300 }
301
302
303
readingFileFailed(const QFile & file,const QString & err_msg)304 void Smb4KNotification::readingFileFailed(const QFile& file, const QString& err_msg)
305 {
306 QString text;
307
308 if (!err_msg.isEmpty())
309 {
310 text = i18n("<p>Reading from file <b>%1</b> failed:</p><p><tt>%2</tt></p>", file.fileName(), err_msg);
311 }
312 else
313 {
314 if (!file.errorString().isEmpty())
315 {
316 text = i18n("<p>Reading from file <b>%1</b> failed:</p><p><tt>%2</tt></p>", file.fileName(), file.errorString());
317 }
318 else
319 {
320 text = i18n("<p>Reading from file <b>%1</b> failed.</p>", file.fileName());
321 }
322 }
323
324 Smb4KNotifier *notification = new Smb4KNotifier("readingFileFailed");
325 notification->setText(text);
326 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-error", KIconLoader::NoGroup, 0,
327 KIconLoader::DefaultState));
328 notification->sendEvent();
329 }
330
331
mkdirFailed(const QDir & dir)332 void Smb4KNotification::mkdirFailed(const QDir& dir)
333 {
334 Smb4KNotifier *notification = new Smb4KNotifier("mkdirFailed");
335 notification->setText(i18n("<p>The following directory could not be created:</p><p><tt>%1</tt></p>", dir.absolutePath()));
336 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-error", KIconLoader::NoGroup, 0, KIconLoader::DefaultState));
337 notification->sendEvent();
338 }
339
340
processError(QProcess::ProcessError error)341 void Smb4KNotification::processError(QProcess::ProcessError error)
342 {
343 QString text;
344
345 switch (error)
346 {
347 case QProcess::FailedToStart:
348 {
349 text = i18n("<p>The process failed to start (error code: <tt>%1</tt>).</p>", error);
350 break;
351 }
352 case QProcess::Crashed:
353 {
354 text = i18n("<p>The process crashed (error code: <tt>%1</tt>).</p>", error);
355 break;
356 }
357 case QProcess::Timedout:
358 {
359 text = i18n("<p>The process timed out (error code: <tt>%1</tt>).</p>", error);
360 break;
361 }
362 case QProcess::WriteError:
363 {
364 text = i18n("<p>Could not write to the process (error code: <tt>%1</tt>).</p>", error);
365 break;
366 }
367 case QProcess::ReadError:
368 {
369 text = i18n("<p>Could not read from the process (error code: <tt>%1</tt>).</p>", error);
370 break;
371 }
372 case QProcess::UnknownError:
373 default:
374 {
375 text = i18n("<p>The process reported an unknown error.</p>");
376 break;
377 }
378 }
379
380 Smb4KNotifier *notification = new Smb4KNotifier("processError");
381 notification->setText(text);
382 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-error", KIconLoader::NoGroup, 0, KIconLoader::DefaultState));
383 notification->sendEvent();
384 }
385
386
actionFailed(int err_code)387 void Smb4KNotification::actionFailed(int err_code)
388 {
389 QString text, err_msg;
390
391 switch (err_code)
392 {
393 case ActionReply::NoResponderError:
394 {
395 err_msg = "NoResponderError";
396 break;
397 }
398 case ActionReply::NoSuchActionError:
399 {
400 err_msg = "NoSuchActionError";
401 break;
402 }
403 case ActionReply::InvalidActionError:
404 {
405 err_msg = "InvalidActionError";
406 break;
407 }
408 case ActionReply::AuthorizationDeniedError:
409 {
410 err_msg = "AuthorizationDeniedError";
411 break;
412 }
413 case ActionReply::UserCancelledError:
414 {
415 err_msg = "UserCancelledError";
416 break;
417 }
418 case ActionReply::HelperBusyError:
419 {
420 err_msg = "HelperBusyError";
421 break;
422 }
423 case ActionReply::AlreadyStartedError:
424 {
425 err_msg = "AlreadyStartedError";
426 break;
427 }
428 case ActionReply::DBusError:
429 {
430 err_msg = "DBusError";
431 break;
432 }
433 case ActionReply::BackendError:
434 {
435 err_msg = "BackendError";
436 break;
437 }
438 default:
439 {
440 break;
441 }
442 }
443
444 if (!err_msg.isEmpty())
445 {
446 text = i18n("<p>Executing an action with root privileges failed (error code: <tt>%1</tt>).</p>", err_msg);
447 }
448 else
449 {
450 text = i18n("<p>Executing an action with root privileges failed.</p>");
451 }
452
453 Smb4KNotifier *notification = new Smb4KNotifier("actionFailed");
454 notification->setText(text);
455 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-error", KIconLoader::NoGroup, 0, KIconLoader::DefaultState));
456 notification->sendEvent();
457 }
458
459
invalidURLPassed()460 void Smb4KNotification::invalidURLPassed()
461 {
462 Smb4KNotifier *notification = new Smb4KNotifier("invalidURL");
463 notification->setText(i18n("<p>The URL that was passed is invalid.</p>"));
464 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-error", KIconLoader::NoGroup, 0, KIconLoader::DefaultState));
465 notification->sendEvent();
466 }
467
468
networkCommunicationFailed(const QString & errorMessage)469 void Smb4KNotification::networkCommunicationFailed(const QString& errorMessage)
470 {
471 Smb4KNotifier *notification = new Smb4KNotifier("networkCommunicationFailed");
472 notification->setText(i18n("The network communication failed with the following error message: <s>%1</s>", errorMessage));
473 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-error", KIconLoader::NoGroup, 0, KIconLoader::DefaultState));
474 notification->sendEvent();
475 }
476
477
478