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