| 1 | /* Copyright (C) 2016 - 2017 Dan Chapman <[email protected]> |
| 2 | |
| 3 | This file is part of Dekko email client for Ubuntu devices |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU General Public License as |
| 7 | published by the Free Software Foundation; either version 2 of |
| 8 | the License or (at your option) version 3 |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | #include "config.hpp" |
| 19 | #include "Paths.h" |
| 20 | #include <QCoreApplication> |
| 21 | #include <QLockFile> |
| 22 | #include <SnapStandardPaths.h> |
| 23 | |
| 24 | Paths::Paths(QObject *parent) : QObject(parent) |
| 25 | { |
| 26 | |
| 27 | } |
| 28 | |
| 29 | QString Paths::iconUrl(const Paths::ActionIcon icon, bool prefix) |
| 30 | { |
| 31 | QString iconName; |
| 32 | switch(icon) { |
| 33 | case NoIcon: |
| 34 | break; |
| 35 | case AttachmentIcon: |
| 36 | iconName = QStringLiteral("attachment.svg"); |
| 37 | break; |
| 38 | case AccountIcon: |
| 39 | iconName = QStringLiteral("account.svg"); |
| 40 | break; |
| 41 | case BackIcon: |
| 42 | iconName = QStringLiteral("back.svg"); |
| 43 | break; |
| 44 | case ContextMenuIcon: |
| 45 | iconName = QStringLiteral("contextual-menu.svg"); |
| 46 | break; |
| 47 | case DeleteIcon: |
| 48 | iconName = QStringLiteral("delete.svg"); |
| 49 | break; |
| 50 | case DraftIcon: |
| 51 | iconName = QStringLiteral("draft.svg"); |
| 52 | break; |
| 53 | case EditIcon: |
| 54 | iconName = QStringLiteral("edit.svg"); |
| 55 | break; |
| 56 | case FolderIcon: |
| 57 | iconName = QStringLiteral("folder.svg"); |
| 58 | break; |
| 59 | case ImportantIcon: |
| 60 | iconName = QStringLiteral("important.svg"); |
| 61 | break; |
| 62 | case CombineInboxIcon: |
| 63 | iconName = QStringLiteral("inbox-all.svg"); |
| 64 | break; |
| 65 | case InboxIcon: |
| 66 | iconName = QStringLiteral("inbox.svg"); |
| 67 | break; |
| 68 | case JunkIcon: |
| 69 | iconName = QStringLiteral("junk.svg"); |
| 70 | break; |
| 71 | case MailForwardIcon: |
| 72 | iconName = QStringLiteral("mail-forward.svg"); |
| 73 | break; |
| 74 | case MailForwardedIcon: |
| 75 | iconName = QStringLiteral("mail-forwarded.svg"); |
| 76 | break; |
| 77 | case MailReadIcon: |
| 78 | iconName = QStringLiteral("mail-read.svg"); |
| 79 | break; |
| 80 | case MailRepliedIcon: |
| 81 | iconName = QStringLiteral("mail-replied-all.svg"); |
| 82 | break; |
| 83 | case MailRepliedAllIcon: |
| 84 | iconName = QStringLiteral("mail-replied.svg"); |
| 85 | break; |
| 86 | case MailReplyAllIcon: |
| 87 | iconName = QStringLiteral("mail-reply-all.svg"); |
| 88 | break; |
| 89 | case MailReplyIcon: |
| 90 | iconName = QStringLiteral("mail-reply.svg"); |
| 91 | break; |
| 92 | case MailUnreadIcon: |
| 93 | iconName = QStringLiteral("mail-unread.svg"); |
| 94 | break; |
| 95 | case NavigationMenuIcon: |
| 96 | iconName = QStringLiteral("navigation-menu.svg"); |
| 97 | break; |
| 98 | case NewContactIcon: |
| 99 | iconName = QStringLiteral("contact-new.svg"); |
| 100 | break; |
| 101 | case UnStarredIcon: |
| 102 | iconName = QStringLiteral("non-starred.svg"); |
| 103 | break; |
| 104 | case NoteIcon: |
| 105 | iconName = QStringLiteral("note.svg"); |
| 106 | break; |
| 107 | case NoneSelectedIcon: |
| 108 | iconName = QStringLiteral("select-none.svg"); |
| 109 | break; |
| 110 | case SearchIcon: |
| 111 | iconName = QStringLiteral("search.svg"); |
| 112 | break; |
| 113 | case SelectIcon: |
| 114 | iconName = QStringLiteral("select.svg"); |
| 115 | break; |
| 116 | case SendIcon: |
| 117 | iconName = QStringLiteral("send.svg"); |
| 118 | break; |
| 119 | case SentIcon: |
| 120 | iconName = QStringLiteral("sent.svg"); |
| 121 | break; |
| 122 | case StarredIcon: |
| 123 | iconName = QStringLiteral("starred.svg"); |
| 124 | break; |
| 125 | case SyncIcon: |
| 126 | iconName = QStringLiteral("sync.svg"); |
| 127 | break; |
| 128 | case TagsIcon: |
| 129 | iconName = QStringLiteral("tags.svg"); |
| 130 | break; |
| 131 | case ViewFullScreenIcon: |
| 132 | iconName = QStringLiteral("view-fullscreen.svg"); |
| 133 | break; |
| 134 | case CalendarTodayIcon: |
| 135 | iconName = QStringLiteral("calendar-today.svg"); |
| 136 | break; |
| 137 | case FilterIcon: |
| 138 | iconName = QStringLiteral("filters.svg"); |
| 139 | break; |
| 140 | case TimerIcon: |
| 141 | iconName = QStringLiteral("timer.svg"); |
| 142 | break; |
| 143 | case ViewListSymbolic: |
| 144 | iconName = QStringLiteral("view-list-symbolic.svg"); |
| 145 | break; |
| 146 | case TickIcon: |
| 147 | iconName = QStringLiteral("tick.svg"); |
| 148 | break; |
| 149 | case UndoIcon: |
| 150 | iconName = QStringLiteral("undo.svg"); |
| 151 | break; |
| 152 | case CopyIcon: |
| 153 | iconName = QStringLiteral("edit-copy.svg"); |
| 154 | break; |
| 155 | case CutIcon: |
| 156 | iconName = QStringLiteral("edit-cut.svg"); |
| 157 | break; |
| 158 | case PasteIcon: |
| 159 | iconName = QStringLiteral("edit-paste.svg"); |
| 160 | break; |
| 161 | case ShareIcon: |
| 162 | iconName = QStringLiteral("share.svg"); |
| 163 | break; |
| 164 | case BrowserIcon: |
| 165 | iconName = QStringLiteral("webbrowser-app-symbolic.svg"); |
| 166 | break; |
| 167 | }; |
| 168 | |
| 169 | if (!iconName.isEmpty()) { |
| 170 | if (prefix) |
| 171 | return QStringLiteral(":/actions/%1").arg(iconName); |
| 172 | else |
| 173 | return QStringLiteral("/actions/%1").arg(iconName); |
| 174 | |
| 175 | } else { |
| 176 | qDebug() << "Unknown icon"; |
| 177 | return QString(); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | QUrl Paths::actionIconUrl(const Paths::ActionIcon icon) |
| 182 | { |
| 183 | return QStringLiteral("qrc://%1").arg(Paths::iconUrl(icon, false)); |
| 184 | } |
| 185 | |
| 186 | QString Paths::userscript(const Paths::UserScript script) |
| 187 | { |
| 188 | switch (script) { |
| 189 | case CidQueryScript: |
| 190 | return findUserScript(QStringLiteral("add_cid_query.js")); |
| 191 | case FakeScript: |
| 192 | return findUserScript(QStringLiteral("fake_script.js")); |
| 193 | case FontScript: |
| 194 | return findUserScript(QStringLiteral("font_user_script.js")); |
| 195 | case OverFlowScript: |
| 196 | return findUserScript(QStringLiteral("hide_overflow.js")); |
| 197 | case ViewportScript: |
| 198 | return findUserScript(QStringLiteral("viewport_shim.js")); |
| 199 | case ZoomScript: |
| 200 | return findUserScript(QStringLiteral("zoom.js")); |
| 201 | } |
| 202 | return QString(); |
| 203 | } |
| 204 | |
| 205 | QString Paths::mimeIconForUrl(const QString &url) |
| 206 | { |
| 207 | static const QString defaultIcon = QStringLiteral("file"); |
| 208 | QString ret; |
| 209 | const QString path = QUrl(url).toLocalFile(); |
| 210 | const QFileInfo info(path); |
| 211 | if (info.exists()) { |
| 212 | const QMimeType type = s_mimeCache.getMimeTypeForFile(path); |
| 213 | ret = s_mimeCache.getIconForMimeType(type.name()); |
| 214 | if (ret.isEmpty()) { |
| 215 | ret = defaultIcon; |
| 216 | } |
| 217 | } else { |
| 218 | ret = defaultIcon; |
| 219 | } |
| 220 | return QStringLiteral("qrc:///mimetypes/%1.svg").arg(ret); |
| 221 | } |
| 222 | |
| 223 | QString Paths::mimeIconForMimeType(const QString &mimeType) |
| 224 | { |
| 225 | qDebug() << "Getting icon for mime type: " << mimeType; |
| 226 | QString ret; |
| 227 | const QMimeType type = s_mimeCache.getMimeTypeForName(mimeType); |
| 228 | qDebug() << "TYPENAME: " << type.name(); |
| 229 | ret = s_mimeCache.getIconForMimeType(type.name()); |
| 230 | if (ret.isEmpty()) { |
| 231 | ret = QStringLiteral("file"); |
| 232 | } |
| 233 | qDebug() << "ICON NAME: " << ret; |
| 234 | return QStringLiteral("qrc:///mimetypes/%1.svg").arg(ret); |
| 235 | } |
| 236 | |
| 237 | QString Paths::cachePath() const |
| 238 | { |
| 239 | return Paths::standardCacheLocation(); |
| 240 | } |
| 241 | |
| 242 | QString Paths::configPath() const |
| 243 | { |
| 244 | return Paths::standardConfigLocation(); |
| 245 | } |
| 246 | |
| 247 | QString Paths::cacheFileLocation(const QString &fileName) |
| 248 | { |
| 249 | return Paths::cacheLocationForFile(fileName); |
| 250 | } |
| 251 | |
| 252 | QString Paths::configFileLocation(const QString &fileName) |
| 253 | { |
| 254 | return Paths::configLocationForFile(fileName); |
| 255 | } |
| 256 | |
| 257 | QString Paths::standardCacheLocation() |
| 258 | { |
| 259 | return SnapStandardPaths::writableLocation(SnapStandardPaths::AppCacheLocation); |
| 260 | } |
| 261 | |
| 262 | QString Paths::standardConfigLocation() |
| 263 | { |
| 264 | return SnapStandardPaths::writableLocation(SnapStandardPaths::AppConfigLocation); |
| 265 | } |
| 266 | |
| 267 | QString Paths::standardDataLocation() |
| 268 | { |
| 269 | return SnapStandardPaths::writableLocation(SnapStandardPaths::AppDataLocation); |
| 270 | } |
| 271 | |
| 272 | QString Paths::homeLocation() |
| 273 | { |
| 274 | return SnapStandardPaths::writableLocation(SnapStandardPaths::HomeLocation); |
| 275 | } |
| 276 | |
| 277 | QString Paths::cacheLocationForFile(const QString &fileName) |
| 278 | { |
| 279 | return Paths::standardCacheLocation().append(QStringLiteral("/%1").arg(fileName)); |
| 280 | } |
| 281 | |
| 282 | QString Paths::configLocationForFile(const QString &fileName) |
| 283 | { |
| 284 | return Paths::standardConfigLocation().append(QStringLiteral("/%1").arg(fileName)); |
| 285 | } |
| 286 | |
| 287 | QString Paths::dataLocationForFile(const QString &fileName) |
| 288 | { |
| 289 | return Paths::standardDataLocation().append(QStringLiteral("/%1").arg(fileName)); |
| 290 | } |
| 291 | |
| 292 | bool Paths::checkForStaleLockFile(QLockFile **lockFile, QString &filePath, QString &errorMessage) |
| 293 | { |
| 294 | QDir dir(Paths::standardConfigLocation()); |
| 295 | if (!dir.exists() && !dir.mkpath(QStringLiteral("."))) { |
| 296 | errorMessage = QStringLiteral("Cannot create directory: %1").arg(dir.path()); |
| 297 | return false; |
| 298 | } |
| 299 | |
| 300 | // Reset to config directory for consistency; avoid depending on this behavior for paths |
| 301 | if (QDir::setCurrent(dir.absolutePath()) && dir.isRelative()) { |
| 302 | dir.setPath(QStringLiteral(".")); |
| 303 | } |
| 304 | QLockFile *lock = new QLockFile(filePath); |
| 305 | *lockFile = lock; |
| 306 | lock->setStaleLockTime(0); |
| 307 | if (!lock->tryLock()) { |
| 308 | if (lock->error() == QLockFile::LockFailedError) { |
| 309 | // This happens if a stale lock file exists and another process uses that PID. |
| 310 | // Try removing the stale file, which will fail if a real process is holding a |
| 311 | // file-level lock. A false error is more problematic than not locking properly |
| 312 | // on corner-case systems. |
| 313 | if (!lock->removeStaleLockFile() || !lock->tryLock()) { |
| 314 | errorMessage = QStringLiteral("Configuration file is already in use"); |
| 315 | return false; |
| 316 | } else |
| 317 | qDebug() << "Removed stale lock file"; |
| 318 | } else { |
| 319 | errorMessage = QStringLiteral("Cannot write configuration file (failed to acquire lock)"); |
| 320 | return false; |
| 321 | } |
| 322 | } |
| 323 | return true; |
| 324 | } |
| 325 | |
| 326 | QString Paths::findProviderFile() |
| 327 | { |
| 328 | static QString configFile = QString(); |
| 329 | |
| 330 | // TODO: make this configurable |
| 331 | const QString filePath = QDir::cleanPath(dataDir() + QStringLiteral("/configuration/serviceProviders.conf")); |
| 332 | if (configFile.isEmpty()) { |
| 333 | if (QFile::exists(filePath)) { |
| 334 | configFile = filePath; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | QString message = QStringLiteral("Config file "); |
| 339 | if (configFile.isEmpty()) { |
| 340 | message += "not "; |
| 341 | } |
| 342 | message += "found"; |
| 343 | qDebug() << message; |
| 344 | return configFile; |
| 345 | } |
| 346 | |
| 347 | QString Paths::providerIconForDomain(const QString &domain) |
| 348 | { |
| 349 | QString icon; |
| 350 | if (domain == QStringLiteral("gmail.com")) { |
| 351 | icon = QStringLiteral("online-account-google.svg"); |
| 352 | } else if (domain == QStringLiteral("outlook.com")) { |
| 353 | icon = QStringLiteral("online-account-outlook.svg"); |
| 354 | } else if (domain == QStringLiteral("yahoo.com")) { |
| 355 | icon = QStringLiteral("online-account-yahoo.svg"); |
| 356 | } else if (domain == QStringLiteral("me.com")) { |
| 357 | icon = QStringLiteral("online-account.svg"); |
| 358 | } else { |
| 359 | icon = QStringLiteral("dekko-app-symbolic.svg"); |
| 360 | } |
| 361 | return QStringLiteral("qrc:/provider/%1").arg(icon); |
| 362 | } |
| 363 | |
| 364 | QObject *Paths::factory(QQmlEngine *engine, QJSEngine *scriptEngine) |
| 365 | { |
| 366 | Q_UNUSED(scriptEngine); |
| 367 | Paths *paths = new Paths(reinterpret_cast<QObject*>(engine)); |
| 368 | return paths; |
| 369 | } |
| 370 | |
| 371 | |
| 372 | QString Paths::findUserScript(const QString &scriptName) |
| 373 | { |
| 374 | QString script; |
| 375 | if (script.isEmpty()) { |
| 376 | QString desktopFile = QDir(QCoreApplication::applicationDirPath()).absoluteFilePath(QStringLiteral("../../../dekko.desktop")); |
| 377 | if (QFile::exists(desktopFile)) { |
| 378 | // we are a click so lets tidy up our manifest path and find qmlfile |
| 379 | QDir clickRoot = QFileInfo(desktopFile).absoluteDir(); |
| 380 | QString myPath = clickRoot.absolutePath() + QLatin1String("/userscripts/") + scriptName; |
| 381 | if (QFile::exists(myPath)) { |
| 382 | script = myPath; |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | if (script.isEmpty()) { |
| 388 | const QString snapPath = dataDir(); |
| 389 | if (QDir(snapPath).exists()) { |
| 390 | QString myPath = snapPath + QLatin1String("/userscripts/") + scriptName; |
| 391 | if (QFile::exists(myPath)) { |
| 392 | script = myPath; |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | // sanity check |
| 397 | if (script.isEmpty()) { |
| 398 | qFatal("Userscript: %s does not exist at any of the standard paths!", qPrintable(scriptName)); |
| 399 | } |
| 400 | return QStringLiteral("file://%1").arg(script); |
| 401 | } |
| 402 | |
| 403 | Paths::MimeTypeCache Paths::s_mimeCache; |
| 404 | |
| 405 | Paths::MimeTypeCache::MimeTypeCache() |
| 406 | { |
| 407 | iconForMimetype.reserve(100); |
| 408 | mapIconToMimeType("image/png", "image"); |
| 409 | mapIconToMimeType("image/jpeg", "image"); |
| 410 | mapIconToMimeType("image/gif", "image"); |
| 411 | |
| 412 | mapIconToMimeType("image/svg", "drawing"); |
| 413 | mapIconToMimeType("image/svg+xml", "drawing"); |
| 414 | mapIconToMimeType("application/vnd.oasis.opendocument.graphics", "drawing"); |
| 415 | |
| 416 | mapIconToMimeType("audio/mpeg", "sound"); |
| 417 | mapIconToMimeType("audio/x-wav", "sound"); |
| 418 | mapIconToMimeType("audio/midi", "sound"); |
| 419 | |
| 420 | mapIconToMimeType("video/mp4", "video"); |
| 421 | |
| 422 | mapIconToMimeType("text/x-csrc", "code"); |
| 423 | mapIconToMimeType("text/x-chdr", "code"); |
| 424 | mapIconToMimeType("text/x-c++src", "code"); |
| 425 | mapIconToMimeType("text/x-c++hdr", "code"); |
| 426 | mapIconToMimeType("text/x-qml", "code"); |
| 427 | mapIconToMimeType("text/x-java", "code"); |
| 428 | mapIconToMimeType("text/css", "code"); |
| 429 | mapIconToMimeType("application/javascript", "code"); |
| 430 | mapIconToMimeType("text/x-diff", "code"); |
| 431 | mapIconToMimeType("text/x-patch", "code"); |
| 432 | |
| 433 | mapIconToMimeType("application/xml", "xml"); |
| 434 | |
| 435 | mapIconToMimeType("application/x-shellscript", "script"); |
| 436 | mapIconToMimeType("application/x-perl", "script"); |
| 437 | |
| 438 | mapIconToMimeType("application/x-object", "binary"); |
| 439 | mapIconToMimeType("application/octet-stream", "binary"); |
| 440 | |
| 441 | mapIconToMimeType("application/x-cd-image", "disk-image"); |
| 442 | |
| 443 | mapIconToMimeType("application/zip", "archive"); |
| 444 | mapIconToMimeType("application/x-xz-compressed-tar", "archive"); |
| 445 | mapIconToMimeType("application/x-compressed-tar", "archive"); |
| 446 | mapIconToMimeType("application/x-rar", "archive"); |
| 447 | mapIconToMimeType("application/x-rpm", "archive"); |
| 448 | mapIconToMimeType("application/gzip", "archive"); |
| 449 | mapIconToMimeType("application/vnd.debian.binary-package", "archive"); |
| 450 | mapIconToMimeType("application/vnd.android.package-archive", "archive"); |
| 451 | mapIconToMimeType("application/x-7z-compressed", "archive"); |
| 452 | mapIconToMimeType("application/x-bzip-compressed-tar", "archive"); |
| 453 | |
| 454 | mapIconToMimeType("text/x-makefile", "text"); |
| 455 | mapIconToMimeType("text/x-log", "text"); |
| 456 | mapIconToMimeType("text/x-theme", "text"); |
| 457 | mapIconToMimeType("text/csv", "text"); |
| 458 | mapIconToMimeType("text/plain", "text"); |
| 459 | mapIconToMimeType("text/vcard", "text"); |
| 460 | mapIconToMimeType("text/markdown", "text"); |
| 461 | mapIconToMimeType("application/json", "text"); |
| 462 | |
| 463 | mapIconToMimeType("application/pdf", "pdf"); |
| 464 | |
| 465 | mapIconToMimeType("application/vnd.oasis.opendocument.text", "document"); |
| 466 | mapIconToMimeType("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "document"); |
| 467 | mapIconToMimeType("application/msword", "document"); |
| 468 | |
| 469 | mapIconToMimeType("application/vnd.oasis.opendocument.spreadsheet", "spreadsheet"); |
| 470 | mapIconToMimeType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "spreadsheet"); |
| 471 | mapIconToMimeType("application/vnd.ms-excel", "spreadsheet"); |
| 472 | mapIconToMimeType("application/ms-excel", "spreadsheet"); |
| 473 | |
| 474 | mapIconToMimeType("application/vnd.oasis.opendocument.presentation", "slideshow"); |
| 475 | mapIconToMimeType("application/vnd.openxmlformats-officedocument.presentationml.presentation", "slideshow"); |
| 476 | mapIconToMimeType("application/vnd.ms-powerpoint", "slideshow"); |
| 477 | |
| 478 | mapIconToMimeType("text/html", "webpage"); |
| 479 | |
| 480 | mapIconToMimeType("application/sql", "database"); |
| 481 | mapIconToMimeType("application/x-sqlite3", "database"); |
| 482 | |
| 483 | mapIconToMimeType("application/x-executable", "executable"); |
| 484 | mapIconToMimeType("application/x-ms-dos-executable", "executable"); |
| 485 | } |
| 486 | |
| 487 | void Paths::MimeTypeCache::mapIconToMimeType(const QString &mimeType, const QString &icon) |
| 488 | { |
| 489 | iconForMimetype.insert(mimeType, icon); |
| 490 | } |
| 491 | |
| 492 | QString Paths::MimeTypeCache::getIconForMimeType(const QString &type) |
| 493 | { |
| 494 | return iconForMimetype.value(type); |
| 495 | } |
| 496 | |
| 497 | QMimeType Paths::MimeTypeCache::getMimeTypeForFile(const QString &path) |
| 498 | { |
| 499 | return mimeDb.mimeTypeForFile(path); |
| 500 | } |
| 501 | |
| 502 | QMimeType Paths::MimeTypeCache::getMimeTypeForName(const QString &name) |
| 503 | { |
| 504 | return mimeDb.mimeTypeForName(name); |
| 505 | } |