Loading

Paste #pfugiqief

  1. /***********************************************************************************
  2. * Adjustable Clock: Plasmoid to show date and time in adjustable format.
  3. * Copyright (C) 2008 - 2013 Michal Dutkiewicz aka Emdek <emdeck@gmail.com>
  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
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  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, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  18. *
  19. ***********************************************************************************/
  20.  
  21. #include "Applet.h"
  22. #include "Clock.h"
  23. #include "ThemeWidget.h"
  24. #include "Configuration.h"
  25.  
  26. #include <QtCore/QDir>
  27. #include <QtGui/QClipboard>
  28. #include <QtGui/QGraphicsLinearLayout>
  29.  
  30. #include <KMenu>
  31. #include <KLocale>
  32. #include <KConfigDialog>
  33. #include <KStandardDirs>
  34.  
  35. #include <Plasma/Package>
  36.  
  37. K_EXPORT_PLASMA_APPLET(adjustableclock, AdjustableClock::Applet)
  38.  
  39. namespace AdjustableClock
  40. {
  41.  
  42. void logMessage(const QString &message)
  43. {
  44.     QFile file(QString("%1/adjustableclock.log").arg(QDir::homePath()));
  45.     file.open(QIODevice::WriteOnly | QIODevice::Append);
  46.  
  47.     QTextStream stream(&file);
  48.     stream << QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss - ") << message << '\n';
  49. }
  50.  
  51. Applet::Applet(QObject *parent, const QVariantList &args) : ClockApplet(parent, args),
  52.     m_source(new DataSource(this)),
  53.     m_clock(new Clock(m_source, false)),
  54.     m_widget(new ThemeWidget(m_clock, this)),
  55.     m_clipboardAction(NULL)
  56. {
  57.     logMessage("Constructor called");
  58.  
  59.     KGlobal::locale()->insertCatalog("libplasmaclock");
  60.     KGlobal::locale()->insertCatalog("timezones4");
  61.     KGlobal::locale()->insertCatalog("plasma_applet_adjustableclock");
  62.  
  63.     QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Horizontal, this);
  64.     layout->setSpacing(0);
  65.     layout->setContentsMargins(0, 0, 0, 0);
  66.     layout->addItem(m_widget);
  67.  
  68.     setLayout(layout);
  69.     setHasConfigurationInterface(true);
  70.     setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
  71.     resize(150, 100);
  72. }
  73.  
  74. void Applet::init()
  75. {
  76.     logMessage("Init called");
  77.  
  78.     ClockApplet::init();
  79.  
  80.     connect(this, SIGNAL(activate()), this, SLOT(copyToClipboard()));
  81. }
  82.  
  83. void Applet::mousePressEvent(QGraphicsSceneMouseEvent *event)
  84. {
  85.     if (event->buttons() == Qt::MidButton) {
  86.         copyToClipboard();
  87.     } else {
  88.         ClockApplet::mousePressEvent(event);
  89.     }
  90. }
  91.  
  92. void Applet::createClockConfigurationInterface(KConfigDialog *parent)
  93. {
  94.     new Configuration(this, new Clock(m_source, true), parent);
  95. }
  96.  
  97. void Applet::clockConfigChanged()
  98. {
  99.     logMessage(QString("ClockConfigChanged called (timezone: %1)").arg(currentTimezone()));
  100.  
  101.     m_source->updateTimeZone();
  102.  
  103.     if (!config().readEntry("themeHtml", QString()).isEmpty()) {
  104.         m_widget->setHtml(QString(), config().readEntry("themeHtml", QString()));
  105.  
  106.         return;
  107.     }
  108.  
  109.     const QString id = config().readEntry("theme", "digital");
  110.     const QStringList locations = KGlobal::dirs()->findDirs("data", "plasma/adjustableclock");
  111.  
  112.     for (int i = 0; i < locations.count(); ++i) {
  113.         const QStringList themes = Plasma::Package::listInstalled(locations.at(i));
  114.  
  115.         for (int j = 0; j < themes.count(); ++j) {
  116.             if (themes.at(j) == id && m_widget->setTheme(locations.at(i) + QDir::separator() + themes.at(j))) {
  117.                 return;
  118.             }
  119.         }
  120.     }
  121.  
  122.     if (!m_widget->setTheme(locations.first() + QDir::separator() + "digital")) {
  123.         m_widget->setHtml(QString(), "<div style=\"text-align: center;\"><span component=\"Hour\">12</span>:<span component=\"Minute\">30</span></div>");
  124.     }
  125. }
  126.  
  127. void Applet::clockConfigAccepted()
  128. {
  129.     emit configNeedsSaving();
  130. }
  131.  
  132. void Applet::changeEngineTimezone(const QString &oldTimeZone, const QString &newTimeZone)
  133. {
  134.     Q_UNUSED(oldTimeZone)
  135.     Q_UNUSED(newTimeZone)
  136.  
  137.     logMessage(QString("ChangeEngineTimezone called (timezone: %1, old: %2, new: %3)").arg(currentTimezone()).arg(oldTimeZone).arg(newTimeZone));
  138.  
  139.     m_source->updateTimeZone();
  140. }
  141.  
  142. void Applet::copyToClipboard()
  143. {
  144.     QApplication::clipboard()->setText(m_clock->evaluate(config().readEntry("fastCopyExpression", "Clock.getValue(Clock.Year) + '-' + Clock.getValue(Clock.Month) + '-' + Clock.getValue(Clock.DayOfMonth) + ' ' + Clock.getValue(Clock.Hour) + ':' + Clock.getValue(Clock.Minute) + ':' + Clock.getValue(Clock.Second)")));
  145. }
  146.  
  147. void Applet::copyToClipboard(QAction *action)
  148. {
  149.     QApplication::clipboard()->setText(action->text());
  150. }
  151.  
  152. void Applet::toolTipAboutToShow()
  153. {
  154.     if (config().keyList().contains("toolTipExpressionMain") || config().keyList().contains("toolTipExpressionSub")) {
  155.         if (config().readEntry("toolTipExpressionMain", QString()).isEmpty() && config().readEntry("toolTipExpressionSub", QString()).isEmpty()) {
  156.             return;
  157.         }
  158.  
  159.         updateToolTipContent();
  160.  
  161.         connect(m_source, SIGNAL(tick()), this, SLOT(updateToolTipContent()));
  162.     } else {
  163.         ClockApplet::toolTipAboutToShow();
  164.     }
  165. }
  166.  
  167. void Applet::toolTipHidden()
  168. {
  169.     disconnect(m_source, SIGNAL(tick()), this, SLOT(updateToolTipContent()));
  170.  
  171.     Plasma::ToolTipManager::self()->clearContent(this);
  172. }
  173.  
  174. void Applet::updateToolTipContent()
  175. {
  176.     Plasma::ToolTipContent toolTipData;
  177.     toolTipData.setImage(KIcon("chronometer").pixmap(IconSize(KIconLoader::Desktop)));
  178.     toolTipData.setMainText(m_clock->evaluate(config().readEntry("toolTipExpressionMain", QString())));
  179.     toolTipData.setSubText(m_clock->evaluate(config().readEntry("toolTipExpressionSub", QString())));
  180.     toolTipData.setAutohide(false);
  181.  
  182.     Plasma::ToolTipManager::self()->setContent(this, toolTipData);
  183. }
  184.  
  185. void Applet::updateClipboardMenu()
  186. {
  187.     qDeleteAll(m_clipboardAction->menu()->actions());
  188.  
  189.     m_clipboardAction->menu()->clear();
  190.  
  191.     const QStringList clipboardExpressions = getClipboardExpressions();
  192.  
  193.     for (int i = 0; i < clipboardExpressions.count(); ++i) {
  194.         if (clipboardExpressions.at(i).isEmpty()) {
  195.             m_clipboardAction->menu()->addSeparator();
  196.         } else {
  197.             m_clipboardAction->menu()->addAction(m_clock->evaluate(clipboardExpressions.at(i)));
  198.         }
  199.     }
  200. }
  201.  
  202. QStringList Applet::getClipboardExpressions() const
  203. {
  204.     QStringList clipboardExpressions;
  205.     clipboardExpressions << "Clock.getValue(Clock.Time, {'short': true})"
  206.     << "Clock.getValue(Clock.Time)"
  207.     << QString()
  208.     << "Clock.getValue(Clock.Date, {'short': true})"
  209.     << "Clock.getValue(Clock.Date)"
  210.     << QString()
  211.     << "Clock.getValue(Clock.DateTime, {'short': true})"
  212.     << "Clock.getValue(Clock.DateTime)"
  213.     << "Clock.getValue(Clock.Year) + '-' + Clock.getValue(Clock.Month) + '-' + Clock.getValue(Clock.DayOfMonth) + ' ' + Clock.getValue(Clock.Hour) + ':' + Clock.getValue(Clock.Minute) + ':' + Clock.getValue(Clock.Second)"
  214.     << QString()
  215.     << "Clock.getValue(Clock.Timestamp)";
  216.  
  217.     return config().readEntry("clipboardExpressions", clipboardExpressions);
  218. }
  219.  
  220. QList<QAction*> Applet::contextualActions()
  221. {
  222.     QList<QAction*> actions = ClockApplet::contextualActions();
  223.  
  224.     if (!m_clipboardAction) {
  225.         m_clipboardAction = new QAction(KIcon("edit-copy"), i18n("C&opy to Clipboard"), this);
  226.         m_clipboardAction->setMenu(new KMenu());
  227.  
  228.         connect(this, SIGNAL(destroyed()), m_clipboardAction->menu(), SLOT(deleteLater()));
  229.         connect(m_clipboardAction->menu(), SIGNAL(aboutToShow()), this, SLOT(updateClipboardMenu()));
  230.         connect(m_clipboardAction->menu(), SIGNAL(triggered(QAction*)), this, SLOT(copyToClipboard(QAction*)));
  231.     }
  232.  
  233.     for (int i = 0; i < actions.count(); ++i) {
  234.         if (actions.at(i)->text() == i18n("C&opy to Clipboard")) {
  235.             actions.removeAt(i);
  236.             actions.insert(i, m_clipboardAction);
  237.  
  238.             m_clipboardAction->setVisible(!getClipboardExpressions().isEmpty());
  239.  
  240.             break;
  241.         }
  242.     }
  243.  
  244.     return actions;
  245. }
  246.  
  247. }

Comments