File indexing completed on 2024-04-14 15:05:30
0001 /*************************************************************************** 0002 This class provides notifications for Smb4K. 0003 ------------------- 0004 begin : Son Jun 27 2010 0005 copyright : (C) 2010-2019 by Alexander Reinholdt 0006 email : alexander.reinholdt@kdemail.net 0007 ***************************************************************************/ 0008 0009 /*************************************************************************** 0010 * This program is free software; you can redistribute it and/or modify * 0011 * it under the terms of the GNU General Public License as published by * 0012 * the Free Software Foundation; either version 2 of the License, or * 0013 * (at your option) any later version. * 0014 * * 0015 * This program is distributed in the hope that it will be useful, but * 0016 * WITHOUT ANY WARRANTY; without even the implied warranty of * 0017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 0018 * General Public License for more details. * 0019 * * 0020 * You should have received a copy of the GNU General Public License * 0021 * along with this program; if not, write to the * 0022 * Free Software Foundation, Inc., 51 Franklin Street, Suite 500, Boston,* 0023 * MA 02110-1335, USA * 0024 ***************************************************************************/ 0025 0026 #ifdef HAVE_CONFIG_H 0027 #include <config.h> 0028 #endif 0029 0030 // application specific includes 0031 #include "smb4knotification.h" 0032 #include "smb4knotification_p.h" 0033 #include "smb4ksettings.h" 0034 #include "smb4kbookmark.h" 0035 #include "smb4kworkgroup.h" 0036 #include "smb4khost.h" 0037 #include "smb4kshare.h" 0038 0039 // KDE includes 0040 #define TRANSLATION_DOMAIN "smb4k-core" 0041 #include <KAuth/KAuthActionReply> 0042 #include <KNotifications/KNotification> 0043 #include <KI18n/KLocalizedString> 0044 #include <KIconThemes/KIconLoader> 0045 #include "kiconthemes_version.h" 0046 0047 using namespace KAuth; 0048 0049 // 0050 // Notifications 0051 // 0052 0053 void Smb4KNotification::shareMounted(const SharePtr &share) 0054 { 0055 Q_ASSERT(share); 0056 0057 if (share) 0058 { 0059 QUrl mountpoint = QUrl::fromLocalFile(share->path()); 0060 0061 Smb4KNotifier *notification = new Smb4KNotifier("shareMounted"); 0062 notification->setText(i18n("<p>The share <b>%1</b> has been mounted to <b>%2</b>.</p>", share->displayString(), share->path())); 0063 #if KICONTHEMES_VERSION < QT_VERSION_CHECK(5,52,0) 0064 notification->setPixmap(KIconLoader::global()->loadIcon("folder-network", KIconLoader::NoGroup, 0, KIconLoader::DefaultState, QStringList("emblem-mounted"))); 0065 #else 0066 notification->setPixmap(KIconLoader::global()->loadIcon("folder-network", KIconLoader::NoGroup, 0, KIconLoader::DefaultState, QStringList({"","emblem-mounted"}))); 0067 #endif 0068 notification->setActions(QStringList(i18n("Open"))); 0069 notification->setMountpoint(mountpoint); 0070 notification->sendEvent(); 0071 } 0072 } 0073 0074 0075 void Smb4KNotification::shareUnmounted(const SharePtr &share) 0076 { 0077 Q_ASSERT(share); 0078 0079 if (share) 0080 { 0081 Smb4KNotifier *notification = new Smb4KNotifier("shareUnmounted"); 0082 notification->setText(i18n("<p>The share <b>%1</b> has been unmounted from <b>%2</b>.</p>", share->displayString(), share->path())); 0083 #if KICONTHEMES_VERSION < QT_VERSION_CHECK(5,52,0) 0084 notification->setPixmap(KIconLoader::global()->loadIcon("folder-network", KIconLoader::NoGroup, 0, KIconLoader::DefaultState, QStringList("emblem-unmounted"))); 0085 #else 0086 notification->setPixmap(KIconLoader::global()->loadIcon("folder-network", KIconLoader::NoGroup, 0, KIconLoader::DefaultState, QStringList({"","emblem-unmounted"}))); 0087 #endif 0088 notification->sendEvent(); 0089 } 0090 } 0091 0092 0093 void Smb4KNotification::sharesMounted(int number) 0094 { 0095 Smb4KNotifier *notification = new Smb4KNotifier("sharesMounted"); 0096 notification->setText(i18np("<p>%1 share has been mounted.</p>", "<p>%1 shares have been mounted.</p>", number)); 0097 notification->setPixmap(KIconLoader::global()->loadIcon("folder-network", KIconLoader::NoGroup, 0, KIconLoader::DefaultState, QStringList("emblem-mounted"))); 0098 notification->sendEvent(); 0099 } 0100 0101 0102 void Smb4KNotification::sharesUnmounted(int number) 0103 { 0104 Smb4KNotifier *notification = new Smb4KNotifier("sharesUnmounted"); 0105 notification->setText(i18np("<p>%1 share has been unmounted.</p>", "<p>%1 shares have been unmounted.</p>", number)); 0106 notification->setPixmap(KIconLoader::global()->loadIcon("folder-network", KIconLoader::NoGroup, 0, KIconLoader::DefaultState, QStringList("emblem-unmounted"))); 0107 notification->sendEvent(); 0108 } 0109 0110 0111 // 0112 // Warnings 0113 // 0114 0115 void Smb4KNotification::openingWalletFailed(const QString& name) 0116 { 0117 Smb4KNotifier *notification = new Smb4KNotifier("openingWalletFailed"); 0118 notification->setText(i18n("<p>Opening the wallet <b>%1</b> failed.</p>", name)); 0119 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-warning", KIconLoader::NoGroup, 0, KIconLoader::DefaultState)); 0120 notification->sendEvent(); 0121 } 0122 0123 0124 void Smb4KNotification::credentialsNotAccessible() 0125 { 0126 Smb4KNotifier *notification = new Smb4KNotifier("credentialsNotAccessible"); 0127 notification->setText(i18n("<p>The credentials stored in the wallet could not be accessed. " 0128 "There is either no wallet available or it could not be opened.</p>")); 0129 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-warning", KIconLoader::NoGroup, 0, KIconLoader::DefaultState)); 0130 notification->sendEvent(); 0131 } 0132 0133 0134 void Smb4KNotification::mimetypeNotSupported(const QString& mimetype) 0135 { 0136 Smb4KNotifier *notification = new Smb4KNotifier("mimetypeNotSupported"); 0137 notification->setText(i18n("<p>The mimetype <b>%1</b> is not supported for printing. " 0138 "Please convert the file to PDF or Postscript and try again.</p>", mimetype)); 0139 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-warning", KIconLoader::NoGroup, 0, KIconLoader::DefaultState)); 0140 notification->sendEvent(); 0141 } 0142 0143 0144 void Smb4KNotification::bookmarkExists(Smb4KBookmark* bookmark) 0145 { 0146 if (bookmark) 0147 { 0148 Smb4KNotifier *notification = new Smb4KNotifier("bookmarkExists"); 0149 notification->setText(i18n("<p>The bookmark for share <b>%1</b> already exists and will be skipped.</p>", 0150 bookmark->displayString())); 0151 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-warning", KIconLoader::NoGroup, 0, KIconLoader::DefaultState)); 0152 notification->sendEvent(); 0153 } 0154 } 0155 0156 0157 void Smb4KNotification::bookmarkLabelInUse(Smb4KBookmark* bookmark) 0158 { 0159 if (bookmark) 0160 { 0161 Smb4KNotifier *notification = new Smb4KNotifier("bookmarkLabelInUse"); 0162 notification->setText(i18n("<p>The label <b>%1</b> of the bookmark for the share <b>%2</b> " 0163 "is already being used and will automatically be renamed.</p>", 0164 bookmark->label(), bookmark->displayString())); 0165 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-warning", KIconLoader::NoGroup, 0, KIconLoader::DefaultState)); 0166 notification->sendEvent(); 0167 } 0168 } 0169 0170 0171 void Smb4KNotification::sambaConfigFileMissing() 0172 { 0173 Smb4KNotifier *notification = new Smb4KNotifier("sambaConfigFileMissing"); 0174 notification->setText(i18n("The configuration file for the Samba suite <b>smb.conf</b> is missing. This is not " 0175 "a fatal error, but you should consider creating one.")); 0176 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-warning", KIconLoader::NoGroup, 0, KIconLoader::DefaultState)); 0177 notification->sendEvent(); 0178 } 0179 0180 0181 // 0182 // Errors 0183 // 0184 void Smb4KNotification::mountingFailed(const SharePtr &share, const QString& err_msg) 0185 { 0186 if (share) 0187 { 0188 QString text; 0189 0190 if (!err_msg.isEmpty()) 0191 { 0192 text = i18n("<p>Mounting the share <b>%1</b> failed:</p><p><tt>%2</tt></p>", share->displayString(), err_msg); 0193 } 0194 else 0195 { 0196 text = i18n("<p>Mounting the share <b>%1</b> failed.</p>", share->displayString()); 0197 } 0198 0199 Smb4KNotifier *notification = new Smb4KNotifier("mountingFailed"); 0200 notification->setText(text); 0201 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-error", KIconLoader::NoGroup, 0, KIconLoader::DefaultState)); 0202 notification->sendEvent(); 0203 } 0204 } 0205 0206 0207 void Smb4KNotification::unmountingFailed(const SharePtr &share, const QString& err_msg) 0208 { 0209 if (share) 0210 { 0211 QString text; 0212 0213 if (!err_msg.isEmpty()) 0214 { 0215 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); 0216 } 0217 else 0218 { 0219 text = i18n("<p>Unmounting the share <b>%1</b> from <b>%2</b> failed.</p>", share->displayString(), share->path()); 0220 } 0221 0222 Smb4KNotifier *notification = new Smb4KNotifier("unmountingFailed"); 0223 notification->setText(text); 0224 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-error", KIconLoader::NoGroup, 0, KIconLoader::DefaultState)); 0225 notification->sendEvent(); 0226 } 0227 } 0228 0229 0230 void Smb4KNotification::unmountingNotAllowed(const SharePtr &share) 0231 { 0232 Q_ASSERT(share); 0233 0234 if (share) 0235 { 0236 Smb4KNotifier *notification = new Smb4KNotifier("unmountingNotAllowed"); 0237 notification->setText(i18n("<p>You are not allowed to unmount the share <b>%1</b> from <b>%2</b>. " 0238 "It is owned by the user <b>%3</b>.</p>", share->displayString(), share->path(), share->user().loginName())); 0239 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-error", KIconLoader::NoGroup, 0, KIconLoader::DefaultState)); 0240 notification->sendEvent(); 0241 } 0242 } 0243 0244 0245 void Smb4KNotification::synchronizationFailed(const QUrl& src, const QUrl& dest, const QString& err_msg) 0246 { 0247 QString text; 0248 0249 if (!err_msg.isEmpty()) 0250 { 0251 text = i18n("<p>Synchronizing <b>%1</b> with <b>%2</b> failed:</p><p><tt>%3</tt></p>", dest.path(), src.path(), err_msg); 0252 } 0253 else 0254 { 0255 text = i18n("<p>Synchronizing <b>%1</b> with <b>%2</b> failed.</p>", dest.path(), src.path()); 0256 } 0257 0258 Smb4KNotifier *notification = new Smb4KNotifier("synchronizationFailed"); 0259 notification->setText(text); 0260 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-error", KIconLoader::NoGroup, 0, KIconLoader::DefaultState)); 0261 notification->sendEvent(); 0262 } 0263 0264 0265 void Smb4KNotification::commandNotFound(const QString& command) 0266 { 0267 Smb4KNotifier *notification = new Smb4KNotifier("commandNotFound"); 0268 notification->setText(i18n("<p>The command <b>%1</b> could not be found. Please check your installation.</p>", command)); 0269 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-error", KIconLoader::NoGroup, 0, KIconLoader::DefaultState)); 0270 notification->sendEvent(); 0271 } 0272 0273 0274 void Smb4KNotification::cannotBookmarkPrinter(const SharePtr &share) 0275 { 0276 if (share && share->isPrinter()) 0277 { 0278 Smb4KNotifier *notification = new Smb4KNotifier("cannotBookmarkPrinter"); 0279 notification->setText(i18n("<p>The share <b>%1</b> is a printer and cannot be bookmarked.</p>", share->displayString())); 0280 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-error", KIconLoader::NoGroup, 0, KIconLoader::DefaultState)); 0281 notification->sendEvent(); 0282 } 0283 } 0284 0285 0286 void Smb4KNotification::fileNotFound(const QString& fileName) 0287 { 0288 Smb4KNotifier *notification = new Smb4KNotifier("fileNotFound"); 0289 notification->setText(i18n("<p>The file <b>%1</b> could not be found.</p>", fileName)); 0290 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-error", KIconLoader::NoGroup, 0, KIconLoader::DefaultState)); 0291 notification->sendEvent(); 0292 } 0293 0294 0295 void Smb4KNotification::openingFileFailed(const QFile& file) 0296 { 0297 QString text; 0298 0299 if (!file.errorString().isEmpty()) 0300 { 0301 text = i18n("<p>Opening the file <b>%1</b> failed:</p><p><tt>%2</tt></p>", file.fileName(), file.errorString()); 0302 } 0303 else 0304 { 0305 text = i18n("<p>Opening the file <b>%1</b> failed.</p>", file.fileName()); 0306 } 0307 0308 Smb4KNotifier *notification = new Smb4KNotifier("openingFileFailed"); 0309 notification->setText(text); 0310 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-error", KIconLoader::NoGroup, 0, KIconLoader::DefaultState)); 0311 notification->sendEvent(); 0312 } 0313 0314 0315 0316 void Smb4KNotification::readingFileFailed(const QFile& file, const QString& err_msg) 0317 { 0318 QString text; 0319 0320 if (!err_msg.isEmpty()) 0321 { 0322 text = i18n("<p>Reading from file <b>%1</b> failed:</p><p><tt>%2</tt></p>", file.fileName(), err_msg); 0323 } 0324 else 0325 { 0326 if (!file.errorString().isEmpty()) 0327 { 0328 text = i18n("<p>Reading from file <b>%1</b> failed:</p><p><tt>%2</tt></p>", file.fileName(), file.errorString()); 0329 } 0330 else 0331 { 0332 text = i18n("<p>Reading from file <b>%1</b> failed.</p>", file.fileName()); 0333 } 0334 } 0335 0336 Smb4KNotifier *notification = new Smb4KNotifier("readingFileFailed"); 0337 notification->setText(text); 0338 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-error", KIconLoader::NoGroup, 0, 0339 KIconLoader::DefaultState)); 0340 notification->sendEvent(); 0341 } 0342 0343 0344 void Smb4KNotification::mkdirFailed(const QDir& dir) 0345 { 0346 Smb4KNotifier *notification = new Smb4KNotifier("mkdirFailed"); 0347 notification->setText(i18n("<p>The following directory could not be created:</p><p><tt>%1</tt></p>", dir.absolutePath())); 0348 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-error", KIconLoader::NoGroup, 0, KIconLoader::DefaultState)); 0349 notification->sendEvent(); 0350 } 0351 0352 0353 void Smb4KNotification::processError(QProcess::ProcessError error) 0354 { 0355 QString text; 0356 0357 switch (error) 0358 { 0359 case QProcess::FailedToStart: 0360 { 0361 text = i18n("<p>The process failed to start (error code: <tt>%1</tt>).</p>", error); 0362 break; 0363 } 0364 case QProcess::Crashed: 0365 { 0366 text = i18n("<p>The process crashed (error code: <tt>%1</tt>).</p>", error); 0367 break; 0368 } 0369 case QProcess::Timedout: 0370 { 0371 text = i18n("<p>The process timed out (error code: <tt>%1</tt>).</p>", error); 0372 break; 0373 } 0374 case QProcess::WriteError: 0375 { 0376 text = i18n("<p>Could not write to the process (error code: <tt>%1</tt>).</p>", error); 0377 break; 0378 } 0379 case QProcess::ReadError: 0380 { 0381 text = i18n("<p>Could not read from the process (error code: <tt>%1</tt>).</p>", error); 0382 break; 0383 } 0384 case QProcess::UnknownError: 0385 default: 0386 { 0387 text = i18n("<p>The process reported an unknown error.</p>"); 0388 break; 0389 } 0390 } 0391 0392 Smb4KNotifier *notification = new Smb4KNotifier("processError"); 0393 notification->setText(text); 0394 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-error", KIconLoader::NoGroup, 0, KIconLoader::DefaultState)); 0395 notification->sendEvent(); 0396 } 0397 0398 0399 void Smb4KNotification::actionFailed(int err_code) 0400 { 0401 QString text, err_msg; 0402 0403 switch (err_code) 0404 { 0405 case ActionReply::NoResponderError: 0406 { 0407 err_msg = "NoResponderError"; 0408 break; 0409 } 0410 case ActionReply::NoSuchActionError: 0411 { 0412 err_msg = "NoSuchActionError"; 0413 break; 0414 } 0415 case ActionReply::InvalidActionError: 0416 { 0417 err_msg = "InvalidActionError"; 0418 break; 0419 } 0420 case ActionReply::AuthorizationDeniedError: 0421 { 0422 err_msg = "AuthorizationDeniedError"; 0423 break; 0424 } 0425 case ActionReply::UserCancelledError: 0426 { 0427 err_msg = "UserCancelledError"; 0428 break; 0429 } 0430 case ActionReply::HelperBusyError: 0431 { 0432 err_msg = "HelperBusyError"; 0433 break; 0434 } 0435 case ActionReply::AlreadyStartedError: 0436 { 0437 err_msg = "AlreadyStartedError"; 0438 break; 0439 } 0440 case ActionReply::DBusError: 0441 { 0442 err_msg = "DBusError"; 0443 break; 0444 } 0445 case ActionReply::BackendError: 0446 { 0447 err_msg = "BackendError"; 0448 break; 0449 } 0450 default: 0451 { 0452 break; 0453 } 0454 } 0455 0456 if (!err_msg.isEmpty()) 0457 { 0458 text = i18n("<p>Executing an action with root privileges failed (error code: <tt>%1</tt>).</p>", err_msg); 0459 } 0460 else 0461 { 0462 text = i18n("<p>Executing an action with root privileges failed.</p>"); 0463 } 0464 0465 Smb4KNotifier *notification = new Smb4KNotifier("actionFailed"); 0466 notification->setText(text); 0467 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-error", KIconLoader::NoGroup, 0, KIconLoader::DefaultState)); 0468 notification->sendEvent(); 0469 } 0470 0471 0472 void Smb4KNotification::invalidURLPassed() 0473 { 0474 Smb4KNotifier *notification = new Smb4KNotifier("invalidURL"); 0475 notification->setText(i18n("<p>The URL that was passed is invalid.</p>")); 0476 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-error", KIconLoader::NoGroup, 0, KIconLoader::DefaultState)); 0477 notification->sendEvent(); 0478 } 0479 0480 0481 void Smb4KNotification::networkCommunicationFailed(const QString& errorMessage) 0482 { 0483 Smb4KNotifier *notification = new Smb4KNotifier("networkCommunicationFailed"); 0484 notification->setText(i18n("The network communication failed with the following error message: <s>%1</s>", errorMessage)); 0485 notification->setPixmap(KIconLoader::global()->loadIcon("dialog-error", KIconLoader::NoGroup, 0, KIconLoader::DefaultState)); 0486 notification->sendEvent(); 0487 } 0488 0489