Browse Source

Reworked code for eject/mount/unmount operations to support new TDEStorageOpResult return type.

Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it>
undefined
Michele Calgaro 1 month ago
parent
commit
39f184eb71
Signed by: Michele Calgaro <michele.calgaro@yahoo.it> GPG Key ID: 2A75B7CA8ADED5CF
4 changed files with 260 additions and 255 deletions
  1. 121
    107
      tdecore/tdehw/disksHelper.cpp
  2. 16
    11
      tdecore/tdehw/disksHelper.h
  3. 110
    121
      tdecore/tdehw/tdestoragedevice.cpp
  4. 13
    16
      tdecore/tdehw/tdestoragedevice.h

+ 121
- 107
tdecore/tdehw/disksHelper.cpp View File

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

+ 16
- 11
tdecore/tdehw/disksHelper.h View File

@@ -21,16 +21,21 @@
21 21
 #define _DISKS_HELPER_H
22 22
 
23 23
 #include <stdlib.h>
24
-
25
-class TDEStorageDevice;
26
-class TQString;
27
-class TQStringList;
28
-
29
-bool ejectDriveUDisks(TDEStorageDevice* sdevice);
30
-bool ejectDriveUDisks2(TDEStorageDevice* sdevice);
31
-int mountDriveUDisks(TQString deviceNode, TQString fileSystemType, TQStringList mountOptions, TQString* errStr = NULL);
32
-int mountDriveUDisks2(TQString deviceNode, TQString fileSystemType, TQString mountOptions, TQString* errStr = NULL);
33
-int unMountDriveUDisks(TQString deviceNode, TQStringList unMountOptions, TQString* errStr = NULL);
34
-int unMountDriveUDisks2(TQString deviceNode, TQString unMountOptions, TQString* errStr = NULL);
24
+#include "tdestoragedevice.h"
25
+
26
+
27
+//-------------------------------
28
+//  UDisks
29
+//-------------------------------
30
+TDEStorageOpResult UDisksEjectDrive(TDEStorageDevice *sdevice);
31
+TDEStorageOpResult UDisksMountDrive(TQString deviceNode, TQString fileSystemType, TQStringList mountOptions);
32
+TDEStorageOpResult UDisksUnmountDrive(TQString deviceNode, TQStringList unmountOptions);
33
+
34
+//-------------------------------
35
+//  UDisks2
36
+//-------------------------------
37
+TDEStorageOpResult UDisks2EjectDrive(TDEStorageDevice *sdevice);
38
+TDEStorageOpResult UDisks2MountDrive(TQString deviceNode, TQString fileSystemType, TQString mountOptions);
39
+TDEStorageOpResult UDisks2UnmountDrive(TQString deviceNode, TQString unmountOptions);
35 40
 
36 41
 #endif

+ 110
- 121
tdecore/tdehw/tdestoragedevice.cpp View File

@@ -28,6 +28,7 @@
28 28
 #include <tqregexp.h>
29 29
 #include <tqpixmap.h>
30 30
 #include <tqfile.h>
31
+#include <tqvariant.h>
31 32
 
32 33
 #include "kdebug.h"
33 34
 #include "tdelocale.h"
@@ -309,7 +310,8 @@ bool TDEStorageDevice::lockDriveMedia(bool lock) {
309 310
 
310 311
 bool TDEStorageDevice::ejectDrive() {
311 312
 	if (!(TDEGlobal::dirs()->findExe("udisksctl").isEmpty())) {
312
-		if (ejectDriveUDisks2(this)) {
313
+		TDEStorageOpResult ejectResult = UDisks2EjectDrive(this);
314
+		if (ejectResult["result"].toBool()) {
313 315
 			return true;
314 316
 		}
315 317
 		else {
@@ -318,7 +320,8 @@ bool TDEStorageDevice::ejectDrive() {
318 320
 		}
319 321
 	}
320 322
 	if (!(TDEGlobal::dirs()->findExe("udisks").isEmpty())) {
321
-		if (ejectDriveUDisks(this)) {
323
+		TDEStorageOpResult ejectResult = UDisksEjectDrive(this);
324
+		if (ejectResult["result"].toBool()) {
322 325
 			return true;
323 326
 		}
324 327
 		else {
@@ -699,15 +702,15 @@ TQString TDEStorageDevice::mountPath() {
699 702
 	return TQString::null;
700 703
 }
701 704
 
702
-TQString TDEStorageDevice::mountDevice(TQString mediaName, TDEStorageMountOptions mountOptions, TQString* errRet, int* retcode) {
703
-	// Device is already mounted
704
-	if (!mountPath().isNull()) {
705
-		return mountPath();
706
-	}
705
+TDEStorageOpResult TDEStorageDevice::mountDevice(TQString mediaName, TDEStorageMountOptions mountOptions) {
706
+	TDEStorageOpResult result;
707 707
 
708
-	int internal_retcode;
709
-	if (!retcode) {
710
-		retcode = &internal_retcode;
708
+	// Check if device is already mounted
709
+	TQString mountpath = mountPath();
710
+	if (!mountpath.isEmpty()) {
711
+		result["mountPath"] = mountpath;
712
+		result["result"] = true;
713
+		return result;
711 714
 	}
712 715
 
713 716
 	TQString devNode = deviceNode();
@@ -716,8 +719,6 @@ TQString TDEStorageDevice::mountDevice(TQString mediaName, TDEStorageMountOption
716 719
 
717 720
 	// Prepare filesystem options for mount
718 721
 	TQStringList udisksOptions;
719
-	TQString optionString;
720
-
721 722
 	if (mountOptions["ro"] == "true") {
722 723
 		udisksOptions.append("ro");
723 724
 	}
@@ -730,38 +731,34 @@ TQString TDEStorageDevice::mountDevice(TQString mediaName, TDEStorageMountOption
730 731
 		udisksOptions.append("sync");
731 732
 	}
732 733
 
733
-	if(  (mountOptions["filesystem"] == "fat")
734
-	  || (mountOptions["filesystem"] == "vfat")
735
-	  || (mountOptions["filesystem"] == "msdos")
736
-	  || (mountOptions["filesystem"] == "umsdos")
737
-	) {
734
+	if ((mountOptions["filesystem"] == "fat") || (mountOptions["filesystem"] == "vfat") ||
735
+	    (mountOptions["filesystem"] == "msdos") || (mountOptions["filesystem"] == "umsdos")) {
738 736
 		if (mountOptions.contains("shortname")) {
739 737
 			udisksOptions.append(TQString("shortname=%1").arg(mountOptions["shortname"]));
740 738
 		}
741 739
 	}
742 740
 
743
-	if( (mountOptions["filesystem"] == "jfs")) {
741
+	if ((mountOptions["filesystem"] == "jfs")) {
744 742
 		if (mountOptions["utf8"] == "true") {
745 743
 			// udisks/udisks2 for now does not support option iocharset= for jfs
746 744
 			// udisksOptions.append("iocharset=utf8");
747 745
 		}
748 746
 	}
749 747
 
750
-	if( (mountOptions["filesystem"] == "ntfs-3g") ) {
748
+	if ((mountOptions["filesystem"] == "ntfs-3g")) {
751 749
 		if (mountOptions.contains("locale")) {
752 750
 			udisksOptions.append(TQString("locale=%1").arg(mountOptions["locale"]));
753 751
 		}
754 752
 	}
755 753
 
756
-	if(  (mountOptions["filesystem"] == "ext3")
757
-	  || (mountOptions["filesystem"] == "ext4")
758
-	) {
754
+	if ((mountOptions["filesystem"] == "ext3") || (mountOptions["filesystem"] == "ext4")) {
759 755
 		if (mountOptions.contains("journaling")) {
760 756
 			// udisks/udisks2 for now does not support option data= for ext3/ext4
761 757
 			// udisksOptions.append(TQString("data=%1").arg(mountOptions["journaling"]));
762 758
 		}
763 759
 	}
764 760
 
761
+	TQString optionString;
765 762
 	for (TQStringList::Iterator it = udisksOptions.begin(); it != udisksOptions.end(); ++it) {
766 763
 		optionString.append(",");
767 764
 		optionString.append(*it);
@@ -772,50 +769,49 @@ TQString TDEStorageDevice::mountDevice(TQString mediaName, TDEStorageMountOption
772 769
 	}
773 770
 
774 771
 	// Try to use UDISKS v2 via DBUS, if available
775
-	TQString errorString;
776 772
 	TQString fileSystemType;
777
-
778 773
 	if (mountOptions.contains("filesystem") && !mountOptions["filesystem"].isEmpty()) {
779 774
 		fileSystemType = mountOptions["filesystem"];
780 775
 	}
781 776
 
782
-	int uDisks2Ret = mountDriveUDisks2(devNode, fileSystemType, optionString, &errorString);
783
-	if (uDisks2Ret == 0) {
777
+	TDEStorageOpResult mountResult = UDisks2MountDrive(devNode, fileSystemType, optionString);
778
+	if (mountResult["result"].toBool()) {
784 779
 		// Update internal mount data
785 780
 		TDEGlobal::hardwareDevices()->processModifiedMounts();
786
-		return mountPath();
781
+		result["mountPath"] = mountPath();
782
+		result["result"] = true;
783
+		return result;
787 784
 	}
788
-	else if (uDisks2Ret == -1) {
789
-		if (errRet) {
790
-			*errRet = errorString;
791
-		}
785
+	else if (mountResult["retcode"].toInt() == -1) {
792 786
 		// Update internal mount data
793 787
 		TDEGlobal::hardwareDevices()->processModifiedMounts();
794
-		return mountPath();
788
+		result["errStr"] = mountResult["errStr"];
789
+		result["result"] = false;
790
+		return result;
795 791
 	}
796 792
 
797 793
 	// The UDISKS v2 DBUS service was either not available or was unusable
798 794
 	// Try to use UDISKS v1 via DBUS, if available
799
-	int uDisksRet = mountDriveUDisks(devNode, fileSystemType, udisksOptions, &errorString);
800
-	if (uDisksRet == 0) {
795
+	mountResult = UDisksMountDrive(devNode, fileSystemType, udisksOptions);
796
+	if (mountResult["result"].toBool()) {
801 797
 		// Update internal mount data
802 798
 		TDEGlobal::hardwareDevices()->processModifiedMounts();
803
-		return mountPath();
799
+		result["mountPath"] = mountPath();
800
+		result["result"] = true;
801
+		return result;
804 802
 	}
805
-	else if (uDisksRet == -1) {
806
-		if (errRet) {
807
-			*errRet = errorString;
808
-		}
803
+	else if (mountResult["retcode"].toInt() == -1) {
809 804
 		// Update internal mount data
810 805
 		TDEGlobal::hardwareDevices()->processModifiedMounts();
811
-		return mountPath();
806
+		result["errStr"] = mountResult["errStr"];
807
+		result["result"] = false;
808
+		return result;
812 809
 	}
813 810
 
814 811
 	// The UDISKS v1 DBUS service was either not available or was unusable
815 812
 	// Use 'udevil' command, if available
816 813
 	TQString command = TQString::null;
817
-	TQString udevilProg = TDEGlobal::dirs()->findExe("udevil");
818
-	if (!udevilProg.isEmpty()) {
814
+	if (!TDEGlobal::dirs()->findExe("udevil").isEmpty()) {
819 815
 		if (mountOptions.contains("filesystem") && !mountOptions["filesystem"].isEmpty()) {
820 816
 			fileSystemType = TQString("-t %1").arg(mountOptions["filesystem"]);
821 817
 		}
@@ -834,8 +830,7 @@ TQString TDEStorageDevice::mountDevice(TQString mediaName, TDEStorageMountOption
834 830
 
835 831
 	// If 'udevil' was not found, use 'pmount' command if available
836 832
 	if(command.isEmpty()) {
837
-		TQString pmountProg = TDEGlobal::dirs()->findExe("pmount");
838
-		if (!pmountProg.isEmpty()) {
833
+		if (!TDEGlobal::dirs()->findExe("pmount").isEmpty()) {
839 834
 			// Create dummy password file
840 835
 			KTempFile passwordFile(TQString::null, "tmp", 0600);
841 836
 			passwordFile.setAutoDelete(true);
@@ -884,39 +879,38 @@ TQString TDEStorageDevice::mountDevice(TQString mediaName, TDEStorageMountOption
884 879
 	}
885 880
 
886 881
 	if(command.isEmpty()) {
887
-		if (errRet) {
888
-			*errRet = i18n("No supported mounting methods were detected on your system");
889
-		}
890
-		return mountPath();
882
+		result["errStr"] = i18n("No supported mounting methods were detected on your system");
883
+		result["result"] = false;
884
+		return result;
891 885
 	}
892 886
 
893 887
 	FILE *exepipe = popen(command.local8Bit(), "r");
894 888
 	if (exepipe) {
895
-		TQString mount_output;
896 889
 		TQTextStream* ts = new TQTextStream(exepipe, IO_ReadOnly);
897
-		mount_output = ts->read();
890
+		TQString mount_output = ts->read();
898 891
 		delete ts;
899
-		*retcode = pclose(exepipe);
900
-		if (errRet) {
901
-			*errRet = mount_output;
902
-		}
892
+		int retcode = pclose(exepipe);
893
+		result["errStr"] = mount_output;
894
+		result["retCode"] = retcode;
903 895
 	}
904 896
 
905 897
 	// Update internal mount data
906 898
 	TDEGlobal::hardwareDevices()->processModifiedMounts();
907
-	return mountPath();
899
+	result["mountPath"] = mountPath();
900
+	result["result"] = !mountPath().isEmpty();
901
+	return result;
908 902
 }
909 903
 
910
-TQString TDEStorageDevice::mountEncryptedDevice(TQString passphrase, TQString mediaName, TDEStorageMountOptions mountOptions, TQString* errRet, int* retcode) {
911
-	int internal_retcode;
912
-	if (!retcode) {
913
-		retcode = &internal_retcode;
914
-	}
915
-
916
-	TQString ret = mountPath();
904
+TDEStorageOpResult TDEStorageDevice::mountEncryptedDevice(TQString passphrase, TQString mediaName,
905
+				TDEStorageMountOptions mountOptions) {
906
+	TDEStorageOpResult result;
917 907
 
918
-	if (!ret.isNull()) {
919
-		return ret;
908
+	// Check if device is already mounted
909
+	TQString mountpath = mountPath();
910
+	if (!mountpath.isEmpty()) {
911
+		result["mountPath"] = mountpath;
912
+		result["result"] = true;
913
+		return result;
920 914
 	}
921 915
 
922 916
 	// Create dummy password file
@@ -924,7 +918,9 @@ TQString TDEStorageDevice::mountEncryptedDevice(TQString passphrase, TQString me
924 918
 	passwordFile.setAutoDelete(true);
925 919
 	TQFile* pwFile = passwordFile.file();
926 920
 	if (!pwFile) {
927
-		return TQString::null;
921
+		result["errStr"] = i18n("Cannot create temporary password file");
922
+		result["result"] = false;
923
+		return result;
928 924
 	}
929 925
 
930 926
 	pwFile->writeBlock(passphrase.ascii(), passphrase.length());
@@ -960,119 +956,112 @@ TQString TDEStorageDevice::mountEncryptedDevice(TQString passphrase, TQString me
960 956
 	passFileName.replace("'", "'\\''");
961 957
 	devNode.replace("'", "'\\''");
962 958
 	mediaName.replace("'", "'\\''");
963
-	TQString command = TQString("pmount -p '%1' %2 '%3' '%4' 2>&1").arg(passFileName).arg(optionString).arg(devNode).arg(mediaName);
959
+	TQString command = TQString("pmount -p '%1' %2 '%3' '%4' 2>&1")
960
+			               .arg(passFileName).arg(optionString).arg(devNode).arg(mediaName);
964 961
 
965 962
 	FILE *exepipe = popen(command.local8Bit(), "r");
966 963
 	if (exepipe) {
967
-		TQString mount_output;
968 964
 		TQTextStream* ts = new TQTextStream(exepipe, IO_ReadOnly);
969
-		mount_output = ts->read();
965
+		TQString mount_output = ts->read();
970 966
 		delete ts;
971
-		*retcode = pclose(exepipe);
972
-		if (errRet) {
973
-			*errRet = mount_output;
974
-		}
967
+		int retcode = pclose(exepipe);
968
+		result["errStr"] = mount_output;
969
+		result["retCode"] = retcode;
975 970
 	}
976 971
 
977 972
 	// Update internal mount data
978 973
 	TDEGlobal::hardwareDevices()->processModifiedMounts();
979
-
980
-	ret = mountPath();
981
-
982
-	return ret;
974
+	result["mountPath"] = mountPath();
975
+	result["result"] = !mountPath().isEmpty();
976
+	return result;
983 977
 }
984 978
 
985
-bool TDEStorageDevice::unmountDevice(TQString* errRet, int* retcode) {
986
-	if (mountPath().isNull()) {
987
-		return true;
988
-	}
989
-
990
-	int internal_retcode;
991
-	if (!retcode) {
992
-		retcode = &internal_retcode;
993
-	}
979
+TDEStorageOpResult TDEStorageDevice::unmountDevice() {
980
+	TDEStorageOpResult result;
994 981
 
982
+	// Check if device is already unmounted
995 983
 	TQString mountpoint = mountPath();
996
-	TQString devNode = deviceNode();
984
+	if (mountpoint.isEmpty()) {
985
+		result["result"] = true;
986
+		return result;
987
+	}
997 988
 
998 989
 	mountpoint.replace("'", "'\\''");
990
+	TQString devNode = deviceNode();
999 991
 
1000 992
 	// Try to use UDISKS v2 via DBUS, if available
1001
-	TQString errorString;
1002
-	int unMountUDisks2Ret = unMountDriveUDisks2(devNode, TQString::null, &errorString);
1003
-	if (unMountUDisks2Ret == 0) {
993
+	TDEStorageOpResult unmountResult = UDisks2UnmountDrive(devNode, TQString::null);
994
+	if (unmountResult["result"].toBool()) {
1004 995
 		// Update internal mount data
1005 996
 		TDEGlobal::hardwareDevices()->processModifiedMounts();
1006
-		return true;
997
+		result["result"] = true;
998
+		return result;
1007 999
 	}
1008
-	else if (unMountUDisks2Ret == -1) {
1009
-		if (errRet) {
1010
-			*errRet = errorString;
1011
-		}
1000
+	else if (unmountResult["retcode"].toInt() == -1) {
1012 1001
 		// Update internal mount data
1013 1002
 		TDEGlobal::hardwareDevices()->processModifiedMounts();
1014
-		return false;
1003
+		result["errStr"] = unmountResult["errStr"];
1004
+		result["result"] = false;
1005
+		return result;
1015 1006
 	}
1016 1007
 
1017 1008
 	// The UDISKS v2 DBUS service was either not available or was unusable
1018 1009
 	// Try to use UDISKS v1 via DBUS, if available
1019
-	int unMountUDisksRet = unMountDriveUDisks(devNode, TQStringList(), &errorString);
1020
-	if (unMountUDisksRet == 0) {
1010
+	unmountResult = UDisksUnmountDrive(devNode, TQStringList());
1011
+	if (unmountResult["result"].toBool()) {
1021 1012
 		// Update internal mount data
1022 1013
 		TDEGlobal::hardwareDevices()->processModifiedMounts();
1023
-		return true;
1014
+		result["result"] = true;
1015
+		return result;
1024 1016
 	}
1025
-	else if (unMountUDisksRet == -1) {
1026
-		if (errRet) {
1027
-			*errRet = errorString;
1028
-		}
1017
+	else if (unmountResult["retcode"].toInt() == -1) {
1029 1018
 		// Update internal mount data
1030 1019
 		TDEGlobal::hardwareDevices()->processModifiedMounts();
1031
-		return false;
1020
+		result["errStr"] = unmountResult["errStr"];
1021
+		result["result"] = false;
1022
+		return result;
1032 1023
 	}
1033 1024
 
1034 1025
 	// The UDISKS v1 DBUS service was either not available or was unusable
1035
-	// Try to use udevil, if available
1036
-	TQString command;
1037
-	if(!(TDEGlobal::dirs()->findExe("udevil").isEmpty())) {
1026
+	// Use 'udevil' command, if available
1027
+	TQString command = TQString::null;
1028
+	if (!TDEGlobal::dirs()->findExe("udevil").isEmpty()) {
1038 1029
 		command = TQString("udevil umount '%1' 2>&1").arg(mountpoint);
1039 1030
 	}
1040 1031
 
1041 1032
 	// If 'udevil' was not found, use 'pmount' command if available
1042
-	if(command.isEmpty() &&
1043
-	   !(TDEGlobal::dirs()->findExe("pumount").isEmpty())) {
1033
+	if(command.isEmpty() && !TDEGlobal::dirs()->findExe("pumount").isEmpty()) {
1044 1034
 		command = TQString("pumount '%1' 2>&1").arg(mountpoint);
1045 1035
 	}
1046 1036
 
1047 1037
 	if(command.isEmpty()) {
1048
-		if (errRet) {
1049
-			*errRet = i18n("No supported unmounting methods were detected on your system");
1050
-		}
1051
-		return false;
1038
+		result["errStr"] = i18n("No supported unmounting methods were detected on your system");
1039
+		result["result"] = false;
1040
+		return result;
1052 1041
 	}
1053 1042
 
1054 1043
 	FILE *exepipe = popen(command.local8Bit(), "r");
1055 1044
 	if (exepipe) {
1056
-		TQString umount_output;
1057 1045
 		TQTextStream* ts = new TQTextStream(exepipe, IO_ReadOnly);
1058
-		umount_output = ts->read();
1046
+		TQString umount_output = ts->read();
1059 1047
 		delete ts;
1060
-		*retcode = pclose(exepipe);
1061
-		if (*retcode == 0) {
1048
+		int retcode = pclose(exepipe);
1049
+		if (retcode == 0) {
1062 1050
 			// Update internal mount data
1063 1051
 			TDEGlobal::hardwareDevices()->processModifiedMounts();
1064
-			return true;
1052
+			result["result"] = true;
1053
+			return result;
1065 1054
 		}
1066 1055
 		else {
1067
-			if (errRet) {
1068
-				*errRet = umount_output;
1069
-			}
1056
+			result["errStr"] = umount_output;
1057
+			result["retCode"] = retcode;
1070 1058
 		}
1071 1059
 	}
1072 1060
 
1073 1061
 	// Update internal mount data
1074 1062
 	TDEGlobal::hardwareDevices()->processModifiedMounts();
1075
-	return false;
1063
+	result["result"] = false;
1064
+	return result;
1076 1065
 }
1077 1066
 
1078 1067
 TQString TDEStorageDevice::determineFileSystemType(TQString path) {

+ 13
- 16
tdecore/tdehw/tdestoragedevice.h View File

@@ -166,7 +166,9 @@ enum TDELUKSResult {
166 166
 };
167 167
 };
168 168
 
169
-typedef TQMap<TQString,TQString> TDEStorageMountOptions;
169
+class TQVariant;
170
+typedef TQMap<TQString, TQString> TDEStorageMountOptions;
171
+typedef TQMap<TQString, TQVariant> TDEStorageOpResult;
170 172
 
171 173
 class TDECORE_EXPORT TDEStorageDevice : public TDEGenericDevice
172 174
 {
@@ -231,36 +233,31 @@ class TDECORE_EXPORT TDEStorageDevice : public TDEGenericDevice
231 233
 		 * Mounts the device if not encrypted
232 234
 		 *
233 235
 		 * @param a TQString containing a requested mount name under /media, if desired
234
-		 * @param a TQString containing any mount options for pmount, if desired
235
-		 * @param a pointer to a TQString which will be populated with any error messages from pmount, if desired
236
-		 * @param a pointer to an integer which will be populated with the return code from pmount, if desired
236
+		 * @param a TDEStorageMountOptions containing any mount options for pmount, if desired
237 237
 		 *
238
-		 * @return a TQString with the mount path, if successful
238
+		 * @return a TDEStorageOpResult object containing information about the operation outcome
239 239
 		 */
240
-		TQString mountDevice(TQString mediaName=TQString::null, TDEStorageMountOptions mountOptions=TDEStorageMountOptions(), TQString* errRet=0, int* retcode=0);
240
+		TDEStorageOpResult mountDevice(TQString mediaName = TQString::null,
241
+		    TDEStorageMountOptions mountOptions = TDEStorageMountOptions());
241 242
 
242 243
 		/**
243 244
 		 * Mounts the encrypted device if the correct passphrase is given
244 245
 		 *
245 246
 		 * @param a TQString containing the passphrase
246 247
 		 * @param a TQString containing a requested mount name under /media, if desired
247
-		 * @param a TQString containing any mount options for pmount, if desired
248
-		 * @param a pointer to a TQString which will be populated with any error messages from pmount, if desired
249
-		 * @param a pointer to an integer which will be populated with the return code from pmount, if desired
248
+		 * @param a TDEStorageMountOptions containing any mount options for pmount, if desired
250 249
 		 *
251
-		 * @return a TQString with the mount path, if successful
250
+		 * @return a TDEStorageOpResult object containing information about the operation outcome
252 251
 		 */
253
-		TQString mountEncryptedDevice(TQString passphrase, TQString mediaName=TQString::null, TDEStorageMountOptions mountOptions=TDEStorageMountOptions(), TQString* errRet=0, int* retcode=0);
252
+		TDEStorageOpResult mountEncryptedDevice(TQString passphrase, TQString mediaName = TQString::null,
253
+				TDEStorageMountOptions mountOptions = TDEStorageMountOptions());
254 254
 
255 255
 		/**
256 256
 		 * Unmounts the device
257 257
 		 *
258
-		 * @param a pointer to a TQString which will be populated with any error messages from pmount, if desired
259
-		 * @param a pointer to an integer which will be populated with the return code from pmount, if desired
260
-		 *
261
-		 * @return true if unmount was successful
258
+		 * @return a TDEStorageOpResult object containing information about the operation outcome
262 259
 		 */
263
-		bool unmountDevice(TQString* errRet, int* retcode=0);
260
+		TDEStorageOpResult unmountDevice();
264 261
 
265 262
 		/**
266 263
 		 * @return a TQString with the mount path, if mounted

Loading…
Cancel
Save