• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdecore
 

tdecore

  • tdecore
  • tdehw
disksHelper.cpp
1 /* This file is part of the TDE libraries
2  Copyright (C) 2012 Timothy Pearson <kb9vqf@pearsoncomputing.net>
3  (C) 2013 Golubev Alexander <fatzer2@gmail.com>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License version 2 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #if defined(WITH_UDISKS) || defined(WITH_UDISKS2)
21  #include <tqdbusdata.h>
22  #include <tqdbusmessage.h>
23  #include <tqdbusproxy.h>
24  #include <tqdbusvariant.h>
25  #include <tqdbusconnection.h>
26  #include <tqdbuserror.h>
27  #include <tqdbusdatamap.h>
28  #include <tqdbusobjectpath.h>
29  #include "tqdbusdatalist.h"
30  #include "tqstring.h"
31 
32  #include "tdelocale.h"
33  #include "tdestoragedevice.h"
34  #include "disksHelper.h"
35 #endif
36 
37 
38 #ifdef WITH_UDISKS
39 //-------------------------------
40 // UDisks
41 //-------------------------------
42 TQStringVariantMap udisksEjectDrive(TDEStorageDevice *sdevice) {
43  TQStringVariantMap result;
44  result["result"] = false;
45 
46  TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
47  if (dbusConn.isConnected()) {
48  TQString blockDeviceString = sdevice->deviceNode();
49  blockDeviceString.replace("/dev/", "");
50  blockDeviceString.replace("-", "_2d");
51  blockDeviceString = "/org/freedesktop/UDisks/devices/" + blockDeviceString;
52 
53  // Eject the drive!
54  TQT_DBusError error;
55  TQT_DBusProxy driveControl("org.freedesktop.UDisks", blockDeviceString, "org.freedesktop.UDisks.Device", dbusConn);
56  if (driveControl.canSend()) {
57  TQValueList<TQT_DBusData> params;
58  TQT_DBusDataList options;
59  params << TQT_DBusData::fromList(options);
60  TQT_DBusMessage reply = driveControl.sendWithReply("DriveEject", params, &error);
61  if (error.isValid()) {
62  // Error!
63  result["errStr"] = error.name() + ": " + error.message();
64  return result;
65  }
66  else {
67  // Eject was successful. Check if the media can be powered off and do so in case
68  TQT_DBusProxy driveInformation("org.freedesktop.UDisks", blockDeviceString,
69  "org.freedesktop.DBus.Properties", dbusConn);
70  params.clear();
71  params << TQT_DBusData::fromString("org.freedesktop.UDisks.Drive") << TQT_DBusData::fromString("DriveCanDetach");
72  TQT_DBusMessage reply = driveInformation.sendWithReply("Get", params, &error);
73  if (error.isValid()) {
74  // Error!
75  result["errStr"] = error.name() + ": " + error.message();
76  return result;
77  }
78 
79  if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
80  bool canPowerOff = reply[0].toVariant().value.toBool();
81  if (!canPowerOff) {
82  // This drive does not support power off. Just return since the eject operation has finished.
83  result["result"] = true;
84  return result;
85  }
86 
87  // Power off the drive!
88  params.clear();
89  TQT_DBusDataMap<TQString> options(TQT_DBusData::Variant);
90  params << TQT_DBusData::fromStringKeyMap(options);
91  TQT_DBusMessage reply = driveControl.sendWithReply("DriveDetach", params, &error);
92  if (error.isValid()) {
93  // Error!
94  result["errStr"] = error.name() + ": " + error.message();
95  return result;
96  }
97  else {
98  result["result"] = true;
99  return result;
100  }
101  }
102  }
103  }
104  }
105  return result;
106 }
107 
108 TQStringVariantMap udisksMountDrive(const TQString &deviceNode, const TQString &fileSystemType, TQStringList mountOptions) {
109  TQStringVariantMap result;
110  result["result"] = false;
111  result["retcode"] = -2;
112 
113  TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
114  if (dbusConn.isConnected()) {
115  TQString blockDeviceString = deviceNode;
116  blockDeviceString.replace("/dev/", "");
117  blockDeviceString.replace("-", "_2d");
118  blockDeviceString = "/org/freedesktop/UDisks/devices/" + blockDeviceString;
119 
120  // Mount the drive!
121  TQT_DBusError error;
122  TQT_DBusProxy driveControl("org.freedesktop.UDisks", blockDeviceString, "org.freedesktop.UDisks.Device", dbusConn);
123  if (driveControl.canSend()) {
124  TQValueList<TQT_DBusData> params;
125  params << TQT_DBusData::fromString(fileSystemType);
126  params << TQT_DBusData::fromList(TQT_DBusDataList(mountOptions));
127  TQT_DBusMessage reply = driveControl.sendWithReply("FilesystemMount", params, &error);
128  if (!error.isValid()) {
129  // Success
130  result["retcode"] = 0;
131  result["result"] = true;
132  return result;
133  }
134  else {
135  // Error!
136  if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") {
137  return result; // Service not installed or unavailable
138  }
139  else {
140  result["errStr"] = error.name() + ": " + error.message();
141  result["retcode"] = -1;
142  return result;
143  }
144  }
145  }
146  }
147  return result;
148 }
149 
150 TQStringVariantMap udisksUnmountDrive(const TQString &deviceNode, TQStringList unmountOptions) {
151  TQStringVariantMap result;
152  result["result"] = false;
153  result["retcode"] = -2;
154 
155  TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
156  if (dbusConn.isConnected()) {
157  TQString blockDeviceString = deviceNode;
158  blockDeviceString.replace("/dev/", "");
159  blockDeviceString.replace("-", "_2d");
160  blockDeviceString = "/org/freedesktop/UDisks/devices/" + blockDeviceString;
161 
162  // Mount the drive!
163  TQT_DBusError error;
164  TQT_DBusProxy driveControl("org.freedesktop.UDisks", blockDeviceString, "org.freedesktop.UDisks.Device", dbusConn);
165  if (driveControl.canSend()) {
166  TQValueList<TQT_DBusData> params;
167  params << TQT_DBusData::fromList(TQT_DBusDataList(unmountOptions));
168  TQT_DBusMessage reply = driveControl.sendWithReply("FilesystemUnmount", params, &error);
169  if (!error.isValid()) {
170  // Success
171  result["retcode"] = 0;
172  result["result"] = true;
173  return result;
174  }
175  else {
176  // Error!
177  if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") {
178  return result; // Service not installed or unavailable
179  }
180  else {
181  result["errStr"] = error.name() + ": " + error.message();
182  result["retcode"] = -1;
183  return result;
184  }
185  }
186  }
187  }
188  return result;
189 }
190 #endif
191 
192 
193 #ifdef WITH_UDISKS2
194 //-------------------------------
195 // UDisks2
196 //-------------------------------
197 TQStringVariantMap udisks2EjectDrive(TDEStorageDevice *sdevice) {
198  TQStringVariantMap result;
199  result["result"] = false;
200 
201  TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
202  if (dbusConn.isConnected()) {
203  TQString blockDeviceString = sdevice->deviceNode();
204  blockDeviceString.replace("/dev/", "");
205  blockDeviceString.replace("-", "_2d");
206  blockDeviceString = "/org/freedesktop/UDisks2/block_devices/" + blockDeviceString;
207  TQT_DBusProxy hardwareControl("org.freedesktop.UDisks2", blockDeviceString, "org.freedesktop.DBus.Properties", dbusConn);
208  if (hardwareControl.canSend()) {
209  // get associated udisks2 drive path
210  TQT_DBusError error;
211  TQValueList<TQT_DBusData> params;
212  params << TQT_DBusData::fromString("org.freedesktop.UDisks2.Block") << TQT_DBusData::fromString("Drive");
213  TQT_DBusMessage reply = hardwareControl.sendWithReply("Get", params, &error);
214  if (error.isValid()) {
215  // Error!
216  result["errStr"] = error.name() + ": " + error.message();
217  return result;
218  }
219 
220  if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
221  TQT_DBusObjectPath driveObjectPath = reply[0].toVariant().value.toObjectPath();
222  if (!driveObjectPath.isValid()) {
223  return result;
224  }
225  error = TQT_DBusError();
226  TQT_DBusProxy driveInformation("org.freedesktop.UDisks2", driveObjectPath,
227  "org.freedesktop.DBus.Properties", dbusConn);
228  // can eject?
229  params.clear();
230  params << TQT_DBusData::fromString("org.freedesktop.UDisks2.Drive") << TQT_DBusData::fromString("Ejectable");
231  TQT_DBusMessage reply = driveInformation.sendWithReply("Get", params, &error);
232  if (error.isValid()) {
233  // Error!
234  result["errStr"] = error.name() + ": " + error.message();
235  return result;
236  }
237 
238  if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
239  bool ejectable = reply[0].toVariant().value.toBool();
240  if (!ejectable) {
241  result["errStr"] = i18n("Media not ejectable");
242  return result;
243  }
244 
245  // Eject the drive!
246  TQT_DBusProxy driveControl("org.freedesktop.UDisks2", driveObjectPath, "org.freedesktop.UDisks2.Drive", dbusConn);
247  params.clear();
248  TQT_DBusDataMap<TQString> options(TQT_DBusData::Variant);
249  params << TQT_DBusData::fromStringKeyMap(options);
250  TQT_DBusMessage reply = driveControl.sendWithReply("Eject", params, &error);
251  if (error.isValid()) {
252  // Error!
253  result["errStr"] = error.name() + ": " + error.message();
254  return result;
255  }
256  else {
257  // Eject was successful. Check if the media can be powered off and do so in case
258  params.clear();
259  params << TQT_DBusData::fromString("org.freedesktop.UDisks2.Drive") << TQT_DBusData::fromString("CanPowerOff");
260  TQT_DBusMessage reply = driveInformation.sendWithReply("Get", params, &error);
261  if (error.isValid()) {
262  // Error!
263  result["errStr"] = error.name() + ": " + error.message();
264  return result;
265  }
266 
267  if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
268  bool canPowerOff = reply[0].toVariant().value.toBool();
269  if (!canPowerOff) {
270  // This drive does not support power off. Just return since the eject operation has finished.
271  result["result"] = true;
272  return result;
273  }
274 
275  // Power off the drive!
276  params.clear();
277  TQT_DBusDataMap<TQString> options(TQT_DBusData::Variant);
278  params << TQT_DBusData::fromStringKeyMap(options);
279  TQT_DBusMessage reply = driveControl.sendWithReply("PowerOff", params, &error);
280  if (error.isValid()) {
281  // Error!
282  result["errStr"] = error.name() + ": " + error.message();
283  return result;
284  }
285  else {
286  result["result"] = true;
287  return result;
288  }
289  }
290  }
291  }
292  }
293  }
294  }
295  return result;
296 }
297 
298 TQStringVariantMap udisks2MountDrive(const TQString &deviceNode, const TQString &fileSystemType, const TQString &mountOptions) {
299  TQStringVariantMap result;
300  result["result"] = false;
301  result["retcode"] = -2;
302 
303  TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
304  if (dbusConn.isConnected()) {
305  TQString blockDeviceString = deviceNode;
306  blockDeviceString.replace("/dev/", "");
307  blockDeviceString.replace("-", "_2d");
308  blockDeviceString = "/org/freedesktop/UDisks2/block_devices/" + blockDeviceString;
309 
310  // Mount the drive!
311  TQT_DBusError error;
312  TQT_DBusProxy driveControl("org.freedesktop.UDisks2", blockDeviceString, "org.freedesktop.UDisks2.Filesystem", dbusConn);
313  if (driveControl.canSend()) {
314  TQValueList<TQT_DBusData> params;
315  TQMap<TQString, TQT_DBusData> optionsMap;
316  if (fileSystemType != "") {
317  optionsMap["fstype"] = (TQT_DBusData::fromString(fileSystemType)).getAsVariantData();
318  }
319  optionsMap["options"] = (TQT_DBusData::fromString(mountOptions)).getAsVariantData();
320  params << TQT_DBusData::fromStringKeyMap(TQT_DBusDataMap<TQString>(optionsMap));
321  TQT_DBusMessage reply = driveControl.sendWithReply("Mount", params, &error);
322  if (!error.isValid()) {
323  // Success
324  result["retcode"] = 0;
325  result["result"] = true;
326  return result;
327  }
328  else {
329  // Error!
330  if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") {
331  return result; // Service not installed or unavailable
332  }
333  else {
334  result["errStr"] = error.name() + ": " + error.message();
335  result["retcode"] = -1;
336  return result;
337  }
338  }
339  }
340  }
341  return result;
342 }
343 
344 TQStringVariantMap udisks2UnmountDrive(const TQString &deviceNode, const TQString &unmountOptions) {
345  TQStringVariantMap result;
346  result["result"] = false;
347  result["retcode"] = -2;
348 
349  TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
350  if (dbusConn.isConnected()) {
351  TQString blockDeviceString = deviceNode;
352  blockDeviceString.replace("/dev/", "");
353  blockDeviceString.replace("-", "_2d");
354  blockDeviceString = "/org/freedesktop/UDisks2/block_devices/" + blockDeviceString;
355 
356  // Mount the drive!
357  TQT_DBusError error;
358  TQT_DBusProxy driveControl("org.freedesktop.UDisks2", blockDeviceString, "org.freedesktop.UDisks2.Filesystem", dbusConn);
359  if (driveControl.canSend()) {
360  TQValueList<TQT_DBusData> params;
361  TQMap<TQString, TQT_DBusData> optionsMap;
362  optionsMap["options"] = (TQT_DBusData::fromString(unmountOptions)).getAsVariantData();
363  params << TQT_DBusData::fromStringKeyMap(TQT_DBusDataMap<TQString>(optionsMap));
364  TQT_DBusMessage reply = driveControl.sendWithReply("Unmount", params, &error);
365  if (!error.isValid()) {
366  // Success
367  result["retcode"] = 0;
368  result["result"] = true;
369  return result;
370  }
371  else {
372  // Error!
373  if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") {
374  return result; // Service not installed or unavailable
375  }
376  else {
377  result["errStr"] = error.name() + ": " + error.message();
378  result["retcode"] = -1;
379  return result;
380  }
381  }
382  }
383  }
384  return result;
385 }
386 
387 TQStringVariantMap udisks2UnlockDrive(const TQString &deviceNode, const TQString &passphrase) {
388  TQStringVariantMap result;
389  result["result"] = false;
390  result["retcode"] = -2;
391 
392  TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
393  if (dbusConn.isConnected()) {
394  TQString blockDeviceString = deviceNode;
395  blockDeviceString.replace("/dev/", "");
396  blockDeviceString.replace("-", "_2d");
397  blockDeviceString = "/org/freedesktop/UDisks2/block_devices/" + blockDeviceString;
398 
399  // Unlock/decrypt the drive!
400  TQT_DBusError error;
401  TQT_DBusProxy driveControl("org.freedesktop.UDisks2", blockDeviceString, "org.freedesktop.UDisks2.Encrypted", dbusConn);
402  if (driveControl.canSend()) {
403  TQValueList<TQT_DBusData> params;
404  params << TQT_DBusData::fromString(passphrase);
405  TQMap<TQString, TQT_DBusVariant> optionsMap;
406  params << TQT_DBusData::fromStringKeyMap(TQT_DBusDataMap<TQString>(optionsMap));
407  TQT_DBusMessage reply = driveControl.sendWithReply("Unlock", params, &error);
408  if (!error.isValid()) {
409  if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
410  TQT_DBusObjectPath deviceObjectPath = reply[0].toObjectPath();
411  if (deviceObjectPath.isValid()) {
412  // Success
413  result["unlockedDevice"] = deviceObjectPath;
414  result["retcode"] = 0;
415  result["result"] = true;
416  return result;
417  }
418  }
419  result["errStr"] = i18n("Unknown error during unlocking operation.");
420  result["retcode"] = -1;
421  return result;
422  }
423  else {
424  // Error!
425  if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") {
426  return result; // Service not installed or unavailable
427  }
428  else {
429  result["errStr"] = error.name() + ": " + error.message();
430  result["retcode"] = -1;
431  return result;
432  }
433  }
434  }
435  }
436  return result;
437 }
438 
439 TQStringVariantMap udisks2LockDrive(const TQString &deviceNode) {
440  TQStringVariantMap result;
441  result["result"] = false;
442  result["retcode"] = -2;
443 
444  TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
445  if (dbusConn.isConnected()) {
446  TQString blockDeviceString = deviceNode;
447  blockDeviceString.replace("/dev/", "");
448  blockDeviceString.replace("-", "_2d");
449  blockDeviceString = "/org/freedesktop/UDisks2/block_devices/" + blockDeviceString;
450 
451  // Lock/encrypt the drive!
452  TQT_DBusError error;
453  TQT_DBusProxy driveControl("org.freedesktop.UDisks2", blockDeviceString, "org.freedesktop.UDisks2.Encrypted", dbusConn);
454  if (driveControl.canSend()) {
455  TQValueList<TQT_DBusData> params;
456  TQMap<TQString, TQT_DBusVariant> optionsMap;
457  params << TQT_DBusData::fromStringKeyMap(TQT_DBusDataMap<TQString>(optionsMap));
458  TQT_DBusMessage reply = driveControl.sendWithReply("Lock", params, &error);
459  if (!error.isValid()) {
460  // Success
461  result["retcode"] = 0;
462  result["result"] = true;
463  return result;
464  }
465  else {
466  // Error!
467  if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") {
468  return result; // Service not installed or unavailable
469  }
470  else {
471  result["errStr"] = error.name() + ": " + error.message();
472  result["retcode"] = -1;
473  return result;
474  }
475  }
476  }
477  }
478  return result;
479 }
480 #endif
TDELocale::i18n
TQString i18n(const char *text)
i18n is the function that does everything you need to translate a string.
Definition: tdelocale.cpp:1976
tdelocale.h

tdecore

Skip menu "tdecore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdecore

Skip menu "tdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.9.1
This website is maintained by Timothy Pearson.