• 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 #include "disksHelper.h"
21 #include "tdelocale.h"
22 #include "tdestoragedevice.h"
23 
24 #if defined(WITH_UDISKS) || defined(WITH_UDISKS2)
25  #include <tqdbusdata.h>
26  #include <tqdbusmessage.h>
27  #include <tqdbusproxy.h>
28  #include <tqdbusvariant.h>
29  #include <tqdbusconnection.h>
30  #include <tqdbuserror.h>
31  #include <tqdbusdatamap.h>
32  #include <tqdbusobjectpath.h>
33  #include "tqdbusdatalist.h"
34 #endif
35 
36 
37 #ifdef WITH_UDISKS
38 //-------------------------------
39 // UDisks
40 //-------------------------------
41 TQStringVariantMap UDisksEjectDrive(TDEStorageDevice *sdevice) {
42  TQStringVariantMap result;
43  result["result"] = false;
44 
45  TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
46  if (dbusConn.isConnected()) {
47  TQString blockDeviceString = sdevice->deviceNode();
48  blockDeviceString.replace("/dev/", "");
49  blockDeviceString.replace("-", "_2d");
50  blockDeviceString = "/org/freedesktop/UDisks/devices/" + blockDeviceString;
51 
52  // Eject the drive!
53  TQT_DBusError error;
54  TQT_DBusProxy driveControl("org.freedesktop.UDisks", blockDeviceString, "org.freedesktop.UDisks.Device", dbusConn);
55  if (driveControl.canSend()) {
56  TQValueList<TQT_DBusData> params;
57  TQT_DBusDataList options;
58  params << TQT_DBusData::fromList(options);
59  TQT_DBusMessage reply = driveControl.sendWithReply("DriveEject", params, &error);
60  if (error.isValid()) {
61  // Error!
62  result["errStr"] = error.name() + ": " + error.message();
63  return result;
64  }
65  else {
66  result["result"] = true;
67  return result;
68  }
69  }
70  }
71  return result;
72 }
73 
74 TQStringVariantMap UDisksMountDrive(TQString deviceNode, TQString fileSystemType, TQStringList mountOptions) {
75  TQStringVariantMap result;
76  result["result"] = false;
77  result["retcode"] = -2;
78 
79  TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
80  if (dbusConn.isConnected()) {
81  TQString blockDeviceString = deviceNode;
82  blockDeviceString.replace("/dev/", "");
83  blockDeviceString.replace("-", "_2d");
84  blockDeviceString = "/org/freedesktop/UDisks/devices/" + blockDeviceString;
85 
86  // Mount the drive!
87  TQT_DBusError error;
88  TQT_DBusProxy driveControl("org.freedesktop.UDisks", blockDeviceString, "org.freedesktop.UDisks.Device", dbusConn);
89  if (driveControl.canSend()) {
90  TQValueList<TQT_DBusData> params;
91  params << TQT_DBusData::fromString(fileSystemType);
92  params << TQT_DBusData::fromList(TQT_DBusDataList(mountOptions));
93  TQT_DBusMessage reply = driveControl.sendWithReply("FilesystemMount", params, &error);
94  if (!error.isValid()) {
95  // Success
96  result["retcode"] = 0;
97  result["result"] = true;
98  return result;
99  }
100  else {
101  // Error!
102  if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") {
103  return result; // Service not installed or unavailable
104  }
105  else {
106  result["errStr"] = error.name() + ": " + error.message();
107  result["retcode"] = -1;
108  return result;
109  }
110  }
111  }
112  }
113  return result;
114 }
115 
116 TQStringVariantMap UDisksUnmountDrive(TQString deviceNode, TQStringList unmountOptions) {
117  TQStringVariantMap result;
118  result["result"] = false;
119  result["retcode"] = -2;
120 
121  TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
122  if (dbusConn.isConnected()) {
123  TQString blockDeviceString = deviceNode;
124  blockDeviceString.replace("/dev/", "");
125  blockDeviceString.replace("-", "_2d");
126  blockDeviceString = "/org/freedesktop/UDisks/devices/" + blockDeviceString;
127 
128  // Mount the drive!
129  TQT_DBusError error;
130  TQT_DBusProxy driveControl("org.freedesktop.UDisks", blockDeviceString, "org.freedesktop.UDisks.Device", dbusConn);
131  if (driveControl.canSend()) {
132  TQValueList<TQT_DBusData> params;
133  params << TQT_DBusData::fromList(TQT_DBusDataList(unmountOptions));
134  TQT_DBusMessage reply = driveControl.sendWithReply("FilesystemUnmount", params, &error);
135  if (!error.isValid()) {
136  // Success
137  result["retcode"] = 0;
138  result["result"] = true;
139  return result;
140  }
141  else {
142  // Error!
143  if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") {
144  return result; // Service not installed or unavailable
145  }
146  else {
147  result["errStr"] = error.name() + ": " + error.message();
148  result["retcode"] = -1;
149  return result;
150  }
151  }
152  }
153  }
154  return result;
155 }
156 #endif
157 
158 
159 #ifdef WITH_UDISKS2
160 //-------------------------------
161 // UDisks2
162 //-------------------------------
163 TQStringVariantMap UDisks2EjectDrive(TDEStorageDevice *sdevice) {
164  TQStringVariantMap result;
165  result["result"] = false;
166 
167  TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
168  if (dbusConn.isConnected()) {
169  TQString blockDeviceString = sdevice->deviceNode();
170  blockDeviceString.replace("/dev/", "");
171  blockDeviceString.replace("-", "_2d");
172  blockDeviceString = "/org/freedesktop/UDisks2/block_devices/" + blockDeviceString;
173  TQT_DBusProxy hardwareControl("org.freedesktop.UDisks2", blockDeviceString, "org.freedesktop.DBus.Properties", dbusConn);
174  if (hardwareControl.canSend()) {
175  // get associated udisks2 drive path
176  TQT_DBusError error;
177  TQValueList<TQT_DBusData> params;
178  params << TQT_DBusData::fromString("org.freedesktop.UDisks2.Block") << TQT_DBusData::fromString("Drive");
179  TQT_DBusMessage reply = hardwareControl.sendWithReply("Get", params, &error);
180  if (error.isValid()) {
181  // Error!
182  result["errStr"] = error.name() + ": " + error.message();
183  return result;
184  }
185 
186  if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
187  TQT_DBusObjectPath driveObjectPath = reply[0].toVariant().value.toObjectPath();
188  if (!driveObjectPath.isValid()) {
189  return result;
190  }
191  error = TQT_DBusError();
192  TQT_DBusProxy driveInformation("org.freedesktop.UDisks2", driveObjectPath,
193  "org.freedesktop.DBus.Properties", dbusConn);
194  // can eject?
195  TQValueList<TQT_DBusData> params;
196  params << TQT_DBusData::fromString("org.freedesktop.UDisks2.Drive") << TQT_DBusData::fromString("Ejectable");
197  TQT_DBusMessage reply = driveInformation.sendWithReply("Get", params, &error);
198  if (error.isValid()) {
199  // Error!
200  result["errStr"] = error.name() + ": " + error.message();
201  return result;
202  }
203 
204  if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
205  bool ejectable = reply[0].toVariant().value.toBool();
206  if (!ejectable) {
207  result["errStr"] = i18n("Media not ejectable");
208  return result;
209  }
210 
211  // Eject the drive!
212  TQT_DBusProxy driveControl("org.freedesktop.UDisks2", driveObjectPath, "org.freedesktop.UDisks2.Drive", dbusConn);
213  TQValueList<TQT_DBusData> params;
214  TQT_DBusDataMap<TQString> options(TQT_DBusData::Variant);
215  params << TQT_DBusData::fromStringKeyMap(options);
216  TQT_DBusMessage reply = driveControl.sendWithReply("Eject", params, &error);
217  if (error.isValid()) {
218  // Error!
219  result["errStr"] = error.name() + ": " + error.message();
220  return result;
221  }
222  else {
223  result["result"] = true;
224  return result;
225  }
226  }
227  }
228  }
229  }
230  return result;
231 }
232 
233 TQStringVariantMap UDisks2MountDrive(TQString deviceNode, TQString fileSystemType, TQString mountOptions) {
234  TQStringVariantMap result;
235  result["result"] = false;
236  result["retcode"] = -2;
237 
238  TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
239  if (dbusConn.isConnected()) {
240  TQString blockDeviceString = deviceNode;
241  blockDeviceString.replace("/dev/", "");
242  blockDeviceString.replace("-", "_2d");
243  blockDeviceString = "/org/freedesktop/UDisks2/block_devices/" + blockDeviceString;
244 
245  // Mount the drive!
246  TQT_DBusError error;
247  TQT_DBusProxy driveControl("org.freedesktop.UDisks2", blockDeviceString, "org.freedesktop.UDisks2.Filesystem", dbusConn);
248  if (driveControl.canSend()) {
249  TQValueList<TQT_DBusData> params;
250  TQMap<TQString, TQT_DBusData> optionsMap;
251  if (fileSystemType != "") {
252  optionsMap["fstype"] = (TQT_DBusData::fromString(fileSystemType)).getAsVariantData();
253  }
254  optionsMap["options"] = (TQT_DBusData::fromString(mountOptions)).getAsVariantData();
255  params << TQT_DBusData::fromStringKeyMap(TQT_DBusDataMap<TQString>(optionsMap));
256  TQT_DBusMessage reply = driveControl.sendWithReply("Mount", params, &error);
257  if (!error.isValid()) {
258  // Success
259  result["retcode"] = 0;
260  result["result"] = true;
261  return result;
262  }
263  else {
264  // Error!
265  if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") {
266  return result; // Service not installed or unavailable
267  }
268  else {
269  result["errStr"] = error.name() + ": " + error.message();
270  result["retcode"] = -1;
271  return result;
272  }
273  }
274  }
275  }
276  return result;
277 }
278 
279 TQStringVariantMap UDisks2UnmountDrive(TQString deviceNode, TQString unmountOptions) {
280  TQStringVariantMap result;
281  result["result"] = false;
282  result["retcode"] = -2;
283 
284  TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
285  if (dbusConn.isConnected()) {
286  TQString blockDeviceString = deviceNode;
287  blockDeviceString.replace("/dev/", "");
288  blockDeviceString.replace("-", "_2d");
289  blockDeviceString = "/org/freedesktop/UDisks2/block_devices/" + blockDeviceString;
290 
291  // Mount the drive!
292  TQT_DBusError error;
293  TQT_DBusProxy driveControl("org.freedesktop.UDisks2", blockDeviceString, "org.freedesktop.UDisks2.Filesystem", dbusConn);
294  if (driveControl.canSend()) {
295  TQValueList<TQT_DBusData> params;
296  TQMap<TQString, TQT_DBusData> optionsMap;
297  optionsMap["options"] = (TQT_DBusData::fromString(unmountOptions)).getAsVariantData();
298  params << TQT_DBusData::fromStringKeyMap(TQT_DBusDataMap<TQString>(optionsMap));
299  TQT_DBusMessage reply = driveControl.sendWithReply("Unmount", params, &error);
300  if (!error.isValid()) {
301  // Success
302  result["retcode"] = 0;
303  result["result"] = true;
304  return result;
305  }
306  else {
307  // Error!
308  if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") {
309  return result; // Service not installed or unavailable
310  }
311  else {
312  result["errStr"] = error.name() + ": " + error.message();
313  result["retcode"] = -1;
314  return result;
315  }
316  }
317  }
318  }
319  return result;
320 }
321 #endif
TDELocale::i18n
TQString i18n(const char *text)
Definition: tdelocale.cpp:1977
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.8.13
This website is maintained by Timothy Pearson.