disksHelper.cpp
00001 /* This file is part of the TDE libraries 00002 Copyright (C) 2012 Timothy Pearson <kb9vqf@pearsoncomputing.net> 00003 (C) 2013 Golubev Alexander <fatzer2@gmail.com> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License version 2 as published by the Free Software Foundation. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "disksHelper.h" 00021 #include "tdelocale.h" 00022 #include "tdestoragedevice.h" 00023 00024 #if defined(WITH_UDISKS) || defined(WITH_UDISKS2) 00025 #include <tqdbusdata.h> 00026 #include <tqdbusmessage.h> 00027 #include <tqdbusproxy.h> 00028 #include <tqdbusvariant.h> 00029 #include <tqdbusconnection.h> 00030 #include <tqdbuserror.h> 00031 #include <tqdbusdatamap.h> 00032 #include <tqdbusobjectpath.h> 00033 #include "tqdbusdatalist.h" 00034 #endif 00035 00036 00037 #ifdef WITH_UDISKS 00038 //------------------------------- 00039 // UDisks 00040 //------------------------------- 00041 TQStringVariantMap UDisksEjectDrive(TDEStorageDevice *sdevice) { 00042 TQStringVariantMap result; 00043 result["result"] = false; 00044 00045 TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus); 00046 if (dbusConn.isConnected()) { 00047 TQString blockDeviceString = sdevice->deviceNode(); 00048 blockDeviceString.replace("/dev/", ""); 00049 blockDeviceString.replace("-", "_2d"); 00050 blockDeviceString = "/org/freedesktop/UDisks/devices/" + blockDeviceString; 00051 00052 // Eject the drive! 00053 TQT_DBusError error; 00054 TQT_DBusProxy driveControl("org.freedesktop.UDisks", blockDeviceString, "org.freedesktop.UDisks.Device", dbusConn); 00055 if (driveControl.canSend()) { 00056 TQValueList<TQT_DBusData> params; 00057 TQT_DBusDataList options; 00058 params << TQT_DBusData::fromList(options); 00059 TQT_DBusMessage reply = driveControl.sendWithReply("DriveEject", params, &error); 00060 if (error.isValid()) { 00061 // Error! 00062 result["errStr"] = error.name() + ": " + error.message(); 00063 return result; 00064 } 00065 else { 00066 result["result"] = true; 00067 return result; 00068 } 00069 } 00070 } 00071 return result; 00072 } 00073 00074 TQStringVariantMap UDisksMountDrive(TQString deviceNode, TQString fileSystemType, TQStringList mountOptions) { 00075 TQStringVariantMap result; 00076 result["result"] = false; 00077 result["retcode"] = -2; 00078 00079 TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus); 00080 if (dbusConn.isConnected()) { 00081 TQString blockDeviceString = deviceNode; 00082 blockDeviceString.replace("/dev/", ""); 00083 blockDeviceString.replace("-", "_2d"); 00084 blockDeviceString = "/org/freedesktop/UDisks/devices/" + blockDeviceString; 00085 00086 // Mount the drive! 00087 TQT_DBusError error; 00088 TQT_DBusProxy driveControl("org.freedesktop.UDisks", blockDeviceString, "org.freedesktop.UDisks.Device", dbusConn); 00089 if (driveControl.canSend()) { 00090 TQValueList<TQT_DBusData> params; 00091 params << TQT_DBusData::fromString(fileSystemType); 00092 params << TQT_DBusData::fromList(TQT_DBusDataList(mountOptions)); 00093 TQT_DBusMessage reply = driveControl.sendWithReply("FilesystemMount", params, &error); 00094 if (!error.isValid()) { 00095 // Success 00096 result["retcode"] = 0; 00097 result["result"] = true; 00098 return result; 00099 } 00100 else { 00101 // Error! 00102 if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") { 00103 return result; // Service not installed or unavailable 00104 } 00105 else { 00106 result["errStr"] = error.name() + ": " + error.message(); 00107 result["retcode"] = -1; 00108 return result; 00109 } 00110 } 00111 } 00112 } 00113 return result; 00114 } 00115 00116 TQStringVariantMap UDisksUnmountDrive(TQString deviceNode, TQStringList unmountOptions) { 00117 TQStringVariantMap result; 00118 result["result"] = false; 00119 result["retcode"] = -2; 00120 00121 TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus); 00122 if (dbusConn.isConnected()) { 00123 TQString blockDeviceString = deviceNode; 00124 blockDeviceString.replace("/dev/", ""); 00125 blockDeviceString.replace("-", "_2d"); 00126 blockDeviceString = "/org/freedesktop/UDisks/devices/" + blockDeviceString; 00127 00128 // Mount the drive! 00129 TQT_DBusError error; 00130 TQT_DBusProxy driveControl("org.freedesktop.UDisks", blockDeviceString, "org.freedesktop.UDisks.Device", dbusConn); 00131 if (driveControl.canSend()) { 00132 TQValueList<TQT_DBusData> params; 00133 params << TQT_DBusData::fromList(TQT_DBusDataList(unmountOptions)); 00134 TQT_DBusMessage reply = driveControl.sendWithReply("FilesystemUnmount", params, &error); 00135 if (!error.isValid()) { 00136 // Success 00137 result["retcode"] = 0; 00138 result["result"] = true; 00139 return result; 00140 } 00141 else { 00142 // Error! 00143 if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") { 00144 return result; // Service not installed or unavailable 00145 } 00146 else { 00147 result["errStr"] = error.name() + ": " + error.message(); 00148 result["retcode"] = -1; 00149 return result; 00150 } 00151 } 00152 } 00153 } 00154 return result; 00155 } 00156 #endif 00157 00158 00159 #ifdef WITH_UDISKS2 00160 //------------------------------- 00161 // UDisks2 00162 //------------------------------- 00163 TQStringVariantMap UDisks2EjectDrive(TDEStorageDevice *sdevice) { 00164 TQStringVariantMap result; 00165 result["result"] = false; 00166 00167 TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus); 00168 if (dbusConn.isConnected()) { 00169 TQString blockDeviceString = sdevice->deviceNode(); 00170 blockDeviceString.replace("/dev/", ""); 00171 blockDeviceString.replace("-", "_2d"); 00172 blockDeviceString = "/org/freedesktop/UDisks2/block_devices/" + blockDeviceString; 00173 TQT_DBusProxy hardwareControl("org.freedesktop.UDisks2", blockDeviceString, "org.freedesktop.DBus.Properties", dbusConn); 00174 if (hardwareControl.canSend()) { 00175 // get associated udisks2 drive path 00176 TQT_DBusError error; 00177 TQValueList<TQT_DBusData> params; 00178 params << TQT_DBusData::fromString("org.freedesktop.UDisks2.Block") << TQT_DBusData::fromString("Drive"); 00179 TQT_DBusMessage reply = hardwareControl.sendWithReply("Get", params, &error); 00180 if (error.isValid()) { 00181 // Error! 00182 result["errStr"] = error.name() + ": " + error.message(); 00183 return result; 00184 } 00185 00186 if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) { 00187 TQT_DBusObjectPath driveObjectPath = reply[0].toVariant().value.toObjectPath(); 00188 if (!driveObjectPath.isValid()) { 00189 return result; 00190 } 00191 error = TQT_DBusError(); 00192 TQT_DBusProxy driveInformation("org.freedesktop.UDisks2", driveObjectPath, 00193 "org.freedesktop.DBus.Properties", dbusConn); 00194 // can eject? 00195 TQValueList<TQT_DBusData> params; 00196 params << TQT_DBusData::fromString("org.freedesktop.UDisks2.Drive") << TQT_DBusData::fromString("Ejectable"); 00197 TQT_DBusMessage reply = driveInformation.sendWithReply("Get", params, &error); 00198 if (error.isValid()) { 00199 // Error! 00200 result["errStr"] = error.name() + ": " + error.message(); 00201 return result; 00202 } 00203 00204 if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) { 00205 bool ejectable = reply[0].toVariant().value.toBool(); 00206 if (!ejectable) { 00207 result["errStr"] = i18n("Media not ejectable"); 00208 return result; 00209 } 00210 00211 // Eject the drive! 00212 TQT_DBusProxy driveControl("org.freedesktop.UDisks2", driveObjectPath, "org.freedesktop.UDisks2.Drive", dbusConn); 00213 TQValueList<TQT_DBusData> params; 00214 TQT_DBusDataMap<TQString> options(TQT_DBusData::Variant); 00215 params << TQT_DBusData::fromStringKeyMap(options); 00216 TQT_DBusMessage reply = driveControl.sendWithReply("Eject", params, &error); 00217 if (error.isValid()) { 00218 // Error! 00219 result["errStr"] = error.name() + ": " + error.message(); 00220 return result; 00221 } 00222 else { 00223 result["result"] = true; 00224 return result; 00225 } 00226 } 00227 } 00228 } 00229 } 00230 return result; 00231 } 00232 00233 TQStringVariantMap UDisks2MountDrive(TQString deviceNode, TQString fileSystemType, TQString mountOptions) { 00234 TQStringVariantMap result; 00235 result["result"] = false; 00236 result["retcode"] = -2; 00237 00238 TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus); 00239 if (dbusConn.isConnected()) { 00240 TQString blockDeviceString = deviceNode; 00241 blockDeviceString.replace("/dev/", ""); 00242 blockDeviceString.replace("-", "_2d"); 00243 blockDeviceString = "/org/freedesktop/UDisks2/block_devices/" + blockDeviceString; 00244 00245 // Mount the drive! 00246 TQT_DBusError error; 00247 TQT_DBusProxy driveControl("org.freedesktop.UDisks2", blockDeviceString, "org.freedesktop.UDisks2.Filesystem", dbusConn); 00248 if (driveControl.canSend()) { 00249 TQValueList<TQT_DBusData> params; 00250 TQMap<TQString, TQT_DBusData> optionsMap; 00251 if (fileSystemType != "") { 00252 optionsMap["fstype"] = (TQT_DBusData::fromString(fileSystemType)).getAsVariantData(); 00253 } 00254 optionsMap["options"] = (TQT_DBusData::fromString(mountOptions)).getAsVariantData(); 00255 params << TQT_DBusData::fromStringKeyMap(TQT_DBusDataMap<TQString>(optionsMap)); 00256 TQT_DBusMessage reply = driveControl.sendWithReply("Mount", params, &error); 00257 if (!error.isValid()) { 00258 // Success 00259 result["retcode"] = 0; 00260 result["result"] = true; 00261 return result; 00262 } 00263 else { 00264 // Error! 00265 if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") { 00266 return result; // Service not installed or unavailable 00267 } 00268 else { 00269 result["errStr"] = error.name() + ": " + error.message(); 00270 result["retcode"] = -1; 00271 return result; 00272 } 00273 } 00274 } 00275 } 00276 return result; 00277 } 00278 00279 TQStringVariantMap UDisks2UnmountDrive(TQString deviceNode, TQString unmountOptions) { 00280 TQStringVariantMap result; 00281 result["result"] = false; 00282 result["retcode"] = -2; 00283 00284 TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus); 00285 if (dbusConn.isConnected()) { 00286 TQString blockDeviceString = deviceNode; 00287 blockDeviceString.replace("/dev/", ""); 00288 blockDeviceString.replace("-", "_2d"); 00289 blockDeviceString = "/org/freedesktop/UDisks2/block_devices/" + blockDeviceString; 00290 00291 // Mount the drive! 00292 TQT_DBusError error; 00293 TQT_DBusProxy driveControl("org.freedesktop.UDisks2", blockDeviceString, "org.freedesktop.UDisks2.Filesystem", dbusConn); 00294 if (driveControl.canSend()) { 00295 TQValueList<TQT_DBusData> params; 00296 TQMap<TQString, TQT_DBusData> optionsMap; 00297 optionsMap["options"] = (TQT_DBusData::fromString(unmountOptions)).getAsVariantData(); 00298 params << TQT_DBusData::fromStringKeyMap(TQT_DBusDataMap<TQString>(optionsMap)); 00299 TQT_DBusMessage reply = driveControl.sendWithReply("Unmount", params, &error); 00300 if (!error.isValid()) { 00301 // Success 00302 result["retcode"] = 0; 00303 result["result"] = true; 00304 return result; 00305 } 00306 else { 00307 // Error! 00308 if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") { 00309 return result; // Service not installed or unavailable 00310 } 00311 else { 00312 result["errStr"] = error.name() + ": " + error.message(); 00313 result["retcode"] = -1; 00314 return result; 00315 } 00316 } 00317 } 00318 } 00319 return result; 00320 } 00321 #endif
Trinity API Reference