• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Applets

devicenotifier.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by Alexis Ménard <darktears31@gmail.com>           *
00003  *                                                                         *
00004  *   This program is free software; you can redistribute it and/or modify  *
00005  *   it under the terms of the GNU General Public License as published by  *
00006  *   the Free Software Foundation; either version 2 of the License, or     *
00007  *   (at your option) any later version.                                   *
00008  *                                                                         *
00009  *   This program is distributed in the hope that it will be useful,       *
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00012  *   GNU General Public License for more details.                          *
00013  *                                                                         *
00014  *   You should have received a copy of the GNU General Public License     *
00015  *   along with this program; if not, write to the                         *
00016  *   Free Software Foundation, Inc.,                                       *
00017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
00018  ***************************************************************************/
00019 
00020 //own
00021 #include "devicenotifier.h"
00022 #include "notifierview.h"
00023 #include "notifierdialog.h"
00024 
00025 //Qt
00026 #include <QGraphicsLinearLayout>
00027 #include <QGraphicsProxyWidget>
00028 #include <QTimer>
00029 
00030 //KDE
00031 #include <KIcon>
00032 #include <KConfigDialog>
00033 #include <KStandardDirs>
00034 #include <KDesktopFile>
00035 #include <kdesktopfileactions.h>
00036 #include <KIconLoader>
00037 
00038 //plasma
00039 #include <Plasma/Dialog>
00040 //use for desktop view
00041 #include <Plasma/IconWidget>
00042 #include <Plasma/Theme>
00043 
00044 //solid
00045 #include <solid/device.h>
00046 #include <solid/storagedrive.h>
00047 
00048 
00049 using namespace Plasma;
00050 using namespace Notifier;
00051 
00052 K_EXPORT_PLASMA_APPLET(devicenotifier, DeviceNotifier)
00053 
00054 DeviceNotifier::DeviceNotifier(QObject *parent, const QVariantList &args)
00055     : Plasma::PopupApplet(parent, args),
00056       m_solidEngine(0),
00057       m_solidDeviceEngine(0),
00058       m_icon(0),
00059       m_iconName(""),
00060       m_dialog(0),
00061       m_numberItems(0),
00062       m_itemsValidity(0)
00063 {
00064     setBackgroundHints(StandardBackground);
00065     setAspectRatioMode(IgnoreAspectRatio);
00066 
00067     // let's initialize the widget
00068     (void)widget();
00069 
00070     resize(widget()->sizeHint());
00071 }
00072 
00073 DeviceNotifier::~DeviceNotifier()
00074 {
00075     delete m_icon;
00076     delete m_dialog;
00077 }
00078 
00079 void DeviceNotifier::init()
00080 {
00081     KConfigGroup cg = config();
00082     m_numberItems = cg.readEntry("NumberItems", 4);
00083     m_itemsValidity = cg.readEntry("ItemsValidity", 5);
00084 
00085     m_solidEngine = dataEngine("hotplug");
00086     m_solidDeviceEngine = dataEngine("soliddevice");
00087 
00088     m_icon = new Plasma::IconWidget(KIcon("device-notifier",NULL), QString());
00089     m_iconName = QString("device-notifier");
00090 
00091     Plasma::ToolTipManager::self()->registerWidget(this);
00092 
00093     setPopupIcon(m_icon->icon());
00094 
00095     //feed the list with what is already reported by the engine
00096 
00097     //connect to engine when a device is plug
00098     connect(m_solidEngine, SIGNAL(sourceAdded(const QString&)),
00099             this, SLOT(onSourceAdded(const QString&)));
00100     connect(m_solidEngine, SIGNAL(sourceRemoved(const QString&)),
00101             this, SLOT(onSourceRemoved(const QString&)));
00102 
00103     fillPreviousDevices();
00104 }
00105 
00106 QWidget *DeviceNotifier::widget()
00107 {
00108     if (!m_dialog) {
00109         m_dialog = new NotifierDialog(this);
00110     }
00111 
00112     return m_dialog->dialog();
00113 }
00114 
00115 void DeviceNotifier::fillPreviousDevices()
00116 {
00117     m_fillingPreviousDevices = true;
00118     foreach (const QString &source, m_solidEngine->sources()) {
00119             Solid::Device device = Solid::Device(source);
00120             Solid::Device parentDevice = device.parent();
00121             Solid::StorageDrive *drive = parentDevice.as<Solid::StorageDrive>();
00122             if (drive && (drive->isHotpluggable() || drive->isRemovable())) {
00123                 onSourceAdded(source);
00124             }
00125     }
00126     m_fillingPreviousDevices = false;
00127 }
00128 
00129 void DeviceNotifier::changeNotifierIcon(const QString& name)
00130 {
00131     if (m_icon && name.isNull()) {
00132         m_icon->setIcon(m_iconName);
00133     } else if (m_icon) {
00134         m_icon->setIcon(name);
00135     }
00136 
00137     setPopupIcon(m_icon->icon());
00138 }
00139 
00140 void DeviceNotifier::popupEvent(bool show)
00141 {
00142     if (show) {
00143         Plasma::ToolTipManager::self()->clearContent(this);
00144     }
00145 }
00146 
00147 void DeviceNotifier::dataUpdated(const QString &source, Plasma::DataEngine::Data data)
00148 {
00149     if (data.size() > 0) {
00150         //data from hotplug engine
00151         if (!data["predicateFiles"].isNull()) {
00152             int nb_actions = 0;
00153             QString lastActionLabel;
00154             foreach (const QString &desktop, data["predicateFiles"].toStringList()) {
00155                 QString filePath = KStandardDirs::locate("data", "solid/actions/" + desktop);
00156                 QList<KServiceAction> services = KDesktopFileActions::userDefinedServices(filePath, true);
00157                 nb_actions += services.size();
00158                 if (services.size() > 0) {
00159                     lastActionLabel = QString(services[0].text());
00160                 }
00161             }
00162             m_dialog->setDeviceData(source,data["predicateFiles"],NotifierDialog::PredicateFilesRole);
00163             m_dialog->setDeviceData(source,data["text"], Qt::DisplayRole);
00164 
00165             //icon name
00166             m_dialog->setDeviceData(source,data["icon"], NotifierDialog::IconNameRole);
00167             //icon data
00168             m_dialog->setDeviceData(source,KIcon(data["icon"].toString()), Qt::DecorationRole);
00169 
00170             if (nb_actions > 1) {
00171                 QString s = i18np("1 action for this device",
00172                                   "%1 actions for this device",
00173                                   nb_actions);
00174                 m_dialog->setDeviceData(source, s, NotifierDialog::ActionRole);
00175             } else {
00176                 m_dialog->setDeviceData(source, lastActionLabel, NotifierDialog::ActionRole);
00177             }
00178 
00179         //data from soliddevice engine
00180         } else {
00181             kDebug() << "DeviceNotifier::solidDeviceEngine updated" << source;
00182             if (data["Device Types"].toStringList().contains("Storage Access")) {
00183                 if (data["Accessible"].toBool() == true) {
00184                     m_dialog->setUnMount(true,source);
00185 
00186                     //set icon to mounted device
00187                     QStringList overlays;
00188                     overlays << "emblem-mounted";
00189                     m_dialog->setDeviceData(source, KIcon(m_dialog->getDeviceData(source,NotifierDialog::IconNameRole).toString(), NULL, overlays), Qt::DecorationRole);
00190                 } else if (data["Device Types"].toStringList().contains("OpticalDisc")) {
00191                     //Unmounted optical drive
00192                     m_dialog->setDeviceData(source, KIcon("media-eject"), Qt::DecorationRole);
00193                     //set icon to unmounted device
00194                     m_dialog->setUnMount(true,source);
00195                     m_dialog->setDeviceData(source, KIcon(m_dialog->getDeviceData(source,NotifierDialog::IconNameRole).toString()), Qt::DecorationRole);
00196                 } else {
00197                     m_dialog->setUnMount(false,source);
00198 
00199                     //set icon to unmounted device
00200                     m_dialog->setDeviceData(source, KIcon(m_dialog->getDeviceData(source,NotifierDialog::IconNameRole).toString()), Qt::DecorationRole);
00201                 }
00202             }
00203             // actions specific for other types of devices will go here
00204         }
00205    }
00206 }
00207 
00208 void DeviceNotifier::notifyDevice(const QString &name)
00209 {
00210     m_lastPlugged<<name;
00211 
00212     if (!m_fillingPreviousDevices) {
00213         showPopup();
00214     }
00215 }
00216 
00217 void DeviceNotifier::toolTipAboutToShow()
00218 {
00219     Plasma::ToolTipContent toolTip;
00220     if (!m_lastPlugged.isEmpty()) {
00221         Solid::Device device(m_lastPlugged.last());
00222 
00223         toolTip.setSubText(i18n("Last plugged in device: %1", device.product()));
00224         toolTip.setImage(KIcon(device.icon()));
00225     } else {
00226         toolTip.setSubText(i18n("No devices plugged in"));
00227         toolTip.setImage(KIcon("device-notifier"));
00228     }
00229 
00230     Plasma::ToolTipManager::self()->setContent(this, toolTip);
00231 }
00232 
00233 void DeviceNotifier::toolTipHidden()
00234 {
00235     Plasma::ToolTipManager::self()->clearContent(this);
00236 }
00237 
00238 void DeviceNotifier::removeLastDeviceNotification(const QString &name)
00239 {
00240     m_lastPlugged.removeAll(name);
00241 }
00242 
00243 void DeviceNotifier::onSourceAdded(const QString &name)
00244 {
00245     kDebug() << "DeviceNotifier:: source added" << name;
00246     if (m_dialog->countDevices() == m_numberItems && m_numberItems != 0) {
00247         QString itemUdi = m_dialog->getDeviceUdi(m_dialog->countDevices() - 1);
00248         //disconnect sources and after (important) remove the row
00249         m_solidDeviceEngine->disconnectSource(itemUdi, this);
00250         m_solidEngine->disconnectSource(itemUdi, this);
00251 
00252         m_dialog->removeDevice(m_dialog->countDevices() - 1);
00253     }
00254 
00255     m_dialog->insertDevice(name);
00256     notifyDevice(name);
00257 
00258     m_solidEngine->connectSource(name, this);
00259     m_solidDeviceEngine->connectSource(name, this);
00260 }
00261 
00262 void DeviceNotifier::onSourceRemoved(const QString &name)
00263 {
00264     m_solidEngine->disconnectSource(name, this);
00265     m_solidDeviceEngine->disconnectSource(name, this);
00266 
00267     m_dialog->removeDevice(name);
00268     removeLastDeviceNotification(name);
00269 }
00270 
00271 #include "devicenotifier.moc"

Applets

Skip menu "Applets"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal