Solid
main.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <kapplication.h>
00021 #include <kaboutdata.h>
00022 #include <kcmdlineargs.h>
00023 #include <kmainwindow.h>
00024 #include <kmenubar.h>
00025 #include <kicon.h>
00026
00027 #include <solid/devicenotifier.h>
00028 #include <solid/device.h>
00029 #include <solid/genericinterface.h>
00030
00031 #include <QtGui>
00032
00033 class SolidItem : public QTreeWidgetItem
00034 {
00035 public:
00036 enum SolidItemType { SolidType = UserType + 42 };
00037
00038 SolidItem(const Solid::Device &device)
00039 : QTreeWidgetItem(SolidType)
00040 {
00041 setText(0, device.udi());
00042 QString icon = device.icon();
00043 if (!icon.isEmpty())
00044 setIcon(0, KIcon(icon));
00045 }
00046 };
00047
00048 class SolidBrowser : public QMainWindow
00049 {
00050 Q_OBJECT
00051 public:
00052 SolidBrowser(QWidget *parent = 0)
00053 : QMainWindow(parent)
00054 {
00055 QWidget *central = new QWidget;
00056 QVBoxLayout *layout = new QVBoxLayout(central);
00057
00058 view = new QTreeWidget;
00059 view->setColumnCount(1);
00060 view->setHeaderLabel("Solid UDI");
00061 connect(view, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
00062 SLOT(currentItemChanged(QTreeWidgetItem*)));
00063
00064 details = new QTextBrowser;
00065
00066 filterCombo = new QComboBox;
00067 QStringList filters = QStringList()
00068 << "No filter"
00069 << "Unknown"
00070 << "GenericInterface"
00071 << "Processor"
00072 << "Block"
00073 << "StorageAccess"
00074 << "StorageDrive"
00075 << "OpticalDrive"
00076 << "StorageVolume"
00077 << "OpticalDisc"
00078 << "Camera"
00079 << "PortableMediaPlayer"
00080 << "NetworkInterface"
00081 << "AcAdapter"
00082 << "Battery"
00083 << "Button"
00084 << "AudioInterface"
00085 << "DvbInterface"
00086 << "Video"
00087 << "SerialInterface"
00088 << "SmartCardReader";
00089 filterCombo->addItems(filters);
00090 connect(filterCombo, SIGNAL(currentIndexChanged(QString)), this, SLOT(populate()));
00091
00092 QHBoxLayout *devicesLayout = new QHBoxLayout;
00093 devicesLayout->addWidget(new QLabel("Devices:"));
00094 devicesLayout->addStretch();
00095 devicesLayout->addWidget(new QLabel("Filter:"));
00096 devicesLayout->addWidget(filterCombo);
00097
00098 layout->addLayout(devicesLayout);
00099 layout->addWidget(view);
00100 layout->addWidget(new QLabel("Details:"));
00101 layout->addWidget(details);
00102
00103 QMenu *editMenu = menuBar()->addMenu("&Edit");
00104 QAction *reloadAction = editMenu->addAction("&Refresh", this, SLOT(populate()));
00105 reloadAction->setShortcut(QKeySequence::Refresh);
00106
00107 setCentralWidget(central);
00108 }
00109
00110 public slots:
00111 void populate();
00112
00113 private slots:
00114 void currentItemChanged(QTreeWidgetItem *current);
00115
00116 private:
00117 QTreeWidget *view;
00118 QTextBrowser *details;
00119 QComboBox *filterCombo;
00120 };
00121
00122 void SolidBrowser::currentItemChanged(QTreeWidgetItem *current)
00123 {
00124 details->clear();
00125
00126
00127 if (!current || current->type() != SolidItem::SolidType)
00128 return;
00129
00130 SolidItem *item = static_cast<SolidItem *>(current);
00131 const QString udi = item->text(0);
00132 details->append("<h3>Details for " + udi + "</h3>");
00133
00134 Solid::Device device(udi);
00135 if (!device.isValid()) {
00136 details->append("<p>Invalid Device (it might have been removed?)</p>");
00137 return;
00138 }
00139
00140 if (Solid::GenericInterface *iface = device.as<Solid::GenericInterface>()) {
00141 QString out = "<table><tr><th>Property</th><th>Value</th></tr>\n";
00142 const QMap<QString, QVariant> allProperties = iface->allProperties();
00143 for (QMap<QString, QVariant>::const_iterator it = allProperties.constBegin();
00144 it != allProperties.constEnd(); ++it) {
00145 QString row;
00146 QVariant val = it.value();
00147 row += "<tr><td align=\"right\">";
00148 row += Qt::escape(it.key());
00149 row += ": </td><td>";
00150 if (val.type() == QVariant::ByteArray) {
00151
00152
00153 row += val.toByteArray().toHex();
00154 } else {
00155 row += Qt::escape(it.value().toString());
00156 }
00157 row += "</td></tr>\n";
00158 out += row;
00159 }
00160 out += "</table>\n";
00161 details->append(out);
00162 }
00163 }
00164
00165 static SolidItem *addParentItems(const Solid::Device &device, QHash<QString, SolidItem *> &deviceHash,
00166 QTreeWidget *view)
00167 {
00168 const QString parentUdi = device.parentUdi();
00169
00170 if (deviceHash.contains(parentUdi))
00171 return deviceHash.value(parentUdi);
00172
00173 Solid::Device parentDevice = device.parent();
00174 SolidItem *parentItem = new SolidItem(parentDevice);
00175 deviceHash[parentUdi] = parentItem;
00176
00177 const QString grandParentUdi = parentDevice.parentUdi();
00178 if (grandParentUdi.isEmpty()) {
00179 view->invisibleRootItem()->addChild(parentItem);
00180 } else {
00181
00182 SolidItem *grandParentItem = addParentItems(parentDevice, deviceHash, view);
00183 grandParentItem->addChild(parentItem);
00184 }
00185 return parentItem;
00186 }
00187
00188 void SolidBrowser::populate()
00189 {
00190
00191 view->clear();
00192
00193 QHash<QString, SolidItem *> deviceHash;
00194
00195
00196 QList<Solid::Device> allDevices;
00197 if (filterCombo->currentIndex() <= 0) {
00198
00199 allDevices = Solid::Device::allDevices();
00200 } else {
00201
00202 allDevices = Solid::Device::listFromType(
00203 Solid::DeviceInterface::stringToType(filterCombo->currentText()));
00204 }
00205
00206
00207 foreach (const Solid::Device &device, allDevices) {
00208 deviceHash[device.udi()] = new SolidItem(device);
00209 }
00210
00211
00212 foreach (const Solid::Device &device, allDevices) {
00213 SolidItem *item = deviceHash[device.udi()];
00214 const QString parentUdi = device.parentUdi();
00215 if (parentUdi.isEmpty()) {
00216 view->invisibleRootItem()->addChild(item);
00217 } else {
00218 SolidItem *parentItem = deviceHash.value(parentUdi);
00219 if (!parentItem)
00220 parentItem = addParentItems(device, deviceHash, view);
00221 parentItem->addChild(item);
00222 }
00223 }
00224
00225 view->expandAll();
00226 }
00227
00228 int main (int argc, char *argv[])
00229 {
00230 KAboutData aboutData("solid-browser",
00231 0,
00232 ki18n("Solid Browser"),
00233 "0.1",
00234 ki18n("Displays a Solid Device Tree"),
00235 KAboutData::License_GPL,
00236 ki18n("(c) 2009 Harald Fernengel"),
00237 ki18n("Simple and quick hack for showing a solid device tree"),
00238 "http://www.kde.org/",
00239 "submit@bugs.kde.org");
00240
00241 KCmdLineArgs::init(argc, argv, &aboutData);
00242 KApplication app;
00243
00244 SolidBrowser browser;
00245 browser.populate();
00246 browser.show();
00247
00248 return app.exec();
00249 }
00250
00251 #include "main.moc"