launcher.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "launcher.h"
  2. void LauncherApplet::externalWidgetSetup() {
  3. QIcon::setThemeName(mCfgMan->mIconTheme);
  4. mExternalWidget = new QPushButton();
  5. static_cast<QPushButton*>(mExternalWidget)->setIconSize(
  6. QSize(mParentPanel->mLauncherIconSize,
  7. mParentPanel->mLauncherIconSize)
  8. );
  9. static_cast<QPushButton*>(mExternalWidget)->setFlat(true);
  10. QStringList launcherData = mEntry.split(':');
  11. if (mEntry.endsWith(".desktop")) {
  12. // Desktop Entry
  13. QString desktopEntryPath;
  14. if (QFile::exists("/usr/share/applications/" + launcherData[1])) {
  15. desktopEntryPath = "/usr/share/applications/" + launcherData[1];
  16. }
  17. else {
  18. QString homeDir = QDir::homePath();
  19. desktopEntryPath = homeDir + "/.local/share/applications/" + launcherData[1];
  20. }
  21. QString exec;
  22. QString iconPath;
  23. QString tooltipLabel;
  24. QSettings desktopFileReader(desktopEntryPath, QSettings::IniFormat);
  25. desktopFileReader.sync();
  26. desktopFileReader.beginGroup("Desktop Entry");
  27. exec = desktopFileReader.value("Exec").toString();
  28. iconPath = desktopFileReader.value("Icon").toString();
  29. tooltipLabel = desktopFileReader.value("Name").toString();
  30. desktopFileReader.endGroup();
  31. // https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-1.0.html#exec-variables
  32. if (exec[exec.length()-2] == "%") {
  33. exec.chop(2);
  34. }
  35. static_cast<QPushButton*>(mExternalWidget)->setIcon(
  36. resolveIconNameOrPath(iconPath)
  37. );
  38. mExternalWidget->setToolTip(tooltipLabel);
  39. connect(static_cast<QPushButton*>(mExternalWidget),
  40. &QPushButton::clicked, this, [this, exec]() {
  41. execute(exec);
  42. });
  43. }
  44. else {
  45. // Arbitrary Executable
  46. QString iconPath = launcherData[2];
  47. static_cast<QPushButton*>(mExternalWidget)->setIcon(
  48. resolveIconNameOrPath(iconPath)
  49. );
  50. if (launcherData.count() == 4) {
  51. mExternalWidget->setToolTip(launcherData[3]);
  52. }
  53. connect(static_cast<QPushButton*>(mExternalWidget),
  54. &QPushButton::clicked, mParentPanel,
  55. [this, launcherData]() {
  56. execute(launcherData[1]);
  57. });
  58. }
  59. }
  60. QIcon LauncherApplet::resolveIconNameOrPath(QString iconNameOrPath) {
  61. if (QIcon::hasThemeIcon(iconNameOrPath)) {
  62. return QIcon::fromTheme(iconNameOrPath);
  63. }
  64. else if (QFile::exists(iconNameOrPath)) {
  65. return QIcon(iconNameOrPath);
  66. }
  67. else {
  68. return QIcon::fromTheme("dialog-question");
  69. }
  70. }
  71. void LauncherApplet::execute(QString cmd) {
  72. QProcess* process = new QProcess(mParentPanel->mExecHolder);
  73. mParentPanel->mProcesses.append(process);
  74. process->start(cmd);
  75. if (mCfgMan->mEnableAnimation) {
  76. QPropertyAnimation* animation = new QPropertyAnimation(mExternalWidget, "iconSize");
  77. animation->setDuration(200);
  78. animation->setStartValue(
  79. static_cast<QPushButton*>(mExternalWidget)->iconSize());
  80. animation->setEndValue(QSize(64, 64));
  81. animation->start();
  82. connect(animation, &QPropertyAnimation::finished, this, [this, animation]() {
  83. if (animation != NULL && mExternalWidget != NULL) {
  84. animation->setDuration(50);
  85. animation->setStartValue(
  86. static_cast<QPushButton*>(mExternalWidget)->iconSize());
  87. animation->setEndValue(QSize(mIconSize, mIconSize));
  88. animation->start();
  89. }
  90. });
  91. }
  92. }
  93. LauncherApplet::LauncherApplet(ConfigManager* cfgMan,
  94. Panel* parentPanel,
  95. QString entry) : StaticApplet(
  96. "org.plainDE.launcher",
  97. cfgMan,
  98. parentPanel
  99. ) {
  100. mIconSize = parentPanel->mLauncherIconSize;
  101. mEntry = entry;
  102. }
  103. LauncherApplet::~LauncherApplet() {
  104. }