00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "tdestoragedevice.h"
00021
00022 #include <unistd.h>
00023 #include <fcntl.h>
00024 #include <sys/stat.h>
00025 #include <sys/ioctl.h>
00026 #include <linux/cdrom.h>
00027
00028 #include <tqregexp.h>
00029 #include <tqpixmap.h>
00030 #include <tqfile.h>
00031
00032 #include "tdelocale.h"
00033 #include "tdeglobal.h"
00034 #include "kiconloader.h"
00035 #include "tdetempfile.h"
00036 #include "kstandarddirs.h"
00037
00038 #include "tdehardwaredevices.h"
00039
00040 #include "config.h"
00041
00042
00043 #if defined(WITH_UDISKS) || defined(WITH_UDISKS2)
00044 #include <tqdbusdata.h>
00045 #include <tqdbusmessage.h>
00046 #include <tqdbusproxy.h>
00047 #include <tqdbusvariant.h>
00048 #include <tqdbusconnection.h>
00049 #include <tqdbuserror.h>
00050 #include <tqdbusdatamap.h>
00051 #include <tqdbusobjectpath.h>
00052 #endif // defined(WITH_UDISKS) || defined(WITH_UDISKS2)
00053 #if defined(WITH_UDISKS)
00054 #include "tqdbusdatalist.h"
00055 #endif // ddefined(WITH_UDISKS)
00056
00057 #if defined(WITH_UDISKS) || defined(WITH_UDISKS2)
00058
00059 TQT_DBusData convertDBUSDataToVariantData(TQT_DBusData);
00060 #endif // defined(WITH_UDISKS) || defined(WITH_UDISKS2)
00061
00062 TDEStorageDevice::TDEStorageDevice(TDEGenericDeviceType::TDEGenericDeviceType dt, TQString dn) : TDEGenericDevice(dt, dn), m_mediaInserted(true) {
00063 m_diskType = TDEDiskDeviceType::Null;
00064 m_diskStatus = TDEDiskDeviceStatus::Null;
00065 }
00066
00067 TDEStorageDevice::~TDEStorageDevice() {
00068 }
00069
00070 TDEDiskDeviceType::TDEDiskDeviceType TDEStorageDevice::diskType() {
00071 return m_diskType;
00072 }
00073
00074 void TDEStorageDevice::internalSetDiskType(TDEDiskDeviceType::TDEDiskDeviceType dt) {
00075 m_diskType = dt;
00076 }
00077
00078 bool TDEStorageDevice::isDiskOfType(TDEDiskDeviceType::TDEDiskDeviceType tf) {
00079 return ((m_diskType&tf)!=TDEDiskDeviceType::Null);
00080 }
00081
00082 TDEDiskDeviceStatus::TDEDiskDeviceStatus TDEStorageDevice::diskStatus() {
00083 return m_diskStatus;
00084 }
00085
00086 void TDEStorageDevice::internalSetDiskStatus(TDEDiskDeviceStatus::TDEDiskDeviceStatus st) {
00087 m_diskStatus = st;
00088 }
00089
00090 bool TDEStorageDevice::checkDiskStatus(TDEDiskDeviceStatus::TDEDiskDeviceStatus sf) {
00091 return ((m_diskStatus&sf)!=(TDEDiskDeviceStatus::TDEDiskDeviceStatus)0);
00092 }
00093
00094 bool TDEStorageDevice::lockDriveMedia(bool lock) {
00095 int fd = open(deviceNode().ascii(), O_RDWR | O_NONBLOCK);
00096 if (fd < 0) {
00097 return false;
00098 }
00099 if (ioctl(fd, CDROM_LOCKDOOR, (lock)?1:0) != 0) {
00100 close(fd);
00101 return false;
00102 }
00103 else {
00104 close(fd);
00105 return true;
00106 }
00107 }
00108
00109 bool ejectDriveUDisks(TDEStorageDevice* sdevice) {
00110 #ifdef WITH_UDISKS
00111 TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
00112 if (dbusConn.isConnected()) {
00113 TQString blockDeviceString = sdevice->deviceNode();
00114 blockDeviceString.replace("/dev/", "");
00115 blockDeviceString = "/org/freedesktop/UDisks/devices/" + blockDeviceString;
00116
00117
00118 TQT_DBusError error;
00119 TQT_DBusProxy driveControl("org.freedesktop.UDisks", blockDeviceString, "org.freedesktop.UDisks.Device", dbusConn);
00120 if (driveControl.canSend()) {
00121 TQValueList<TQT_DBusData> params;
00122 TQT_DBusDataList options;
00123 params << TQT_DBusData::fromList(options);
00124 TQT_DBusMessage reply = driveControl.sendWithReply("DriveEject", params, &error);
00125 if (error.isValid()) {
00126
00127 printf("[ERROR][tdehwlib] ejectDriveUDisks: %s\n", error.name().ascii()); fflush(stdout);
00128 return FALSE;
00129 }
00130 else {
00131 return TRUE;
00132 }
00133 }
00134 }
00135 #endif // WITH_UDISKS
00136 return FALSE;
00137 }
00138
00139 bool ejectDriveUDisks2(TDEStorageDevice* sdevice) {
00140 #ifdef WITH_UDISKS2
00141 TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
00142 if (dbusConn.isConnected()) {
00143 TQString blockDeviceString = sdevice->deviceNode();
00144 blockDeviceString.replace("/dev/", "");
00145 blockDeviceString = "/org/freedesktop/UDisks2/block_devices/" + blockDeviceString;
00146 TQT_DBusProxy hardwareControl("org.freedesktop.UDisks2", blockDeviceString, "org.freedesktop.DBus.Properties", dbusConn);
00147 if (hardwareControl.canSend()) {
00148
00149 TQT_DBusError error;
00150 TQValueList<TQT_DBusData> params;
00151 params << TQT_DBusData::fromString("org.freedesktop.UDisks2.Block") << TQT_DBusData::fromString("Drive");
00152 TQT_DBusMessage reply = hardwareControl.sendWithReply("Get", params, &error);
00153 if (error.isValid()) {
00154
00155 printf("[ERROR][tdehwlib] ejectDriveUDisks2: %s\n", error.name().ascii()); fflush(stdout);
00156 return FALSE;
00157 }
00158 else {
00159 if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
00160 TQT_DBusObjectPath driveObjectPath = reply[0].toVariant().value.toObjectPath();
00161 if (!driveObjectPath.isValid()) {
00162 return FALSE;
00163 }
00164
00165 error = TQT_DBusError();
00166 TQT_DBusProxy driveInformation("org.freedesktop.UDisks2", driveObjectPath, "org.freedesktop.DBus.Properties", dbusConn);
00167
00168 TQValueList<TQT_DBusData> params;
00169 params << TQT_DBusData::fromString("org.freedesktop.UDisks2.Drive") << TQT_DBusData::fromString("Ejectable");
00170 TQT_DBusMessage reply = driveInformation.sendWithReply("Get", params, &error);
00171 if (error.isValid()) {
00172
00173 printf("[ERROR][tdehwlib] ejectDriveUDisks2: %s\n", error.name().ascii()); fflush(stdout);
00174 return FALSE;
00175 }
00176 if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
00177 bool ejectable = reply[0].toVariant().value.toBool();
00178 if (!ejectable) {
00179 return FALSE;
00180 }
00181
00182
00183 TQT_DBusProxy driveControl("org.freedesktop.UDisks2", driveObjectPath, "org.freedesktop.UDisks2.Drive", dbusConn);
00184 TQValueList<TQT_DBusData> params;
00185 TQT_DBusDataMap<TQString> options(TQT_DBusData::Variant);
00186 params << TQT_DBusData::fromStringKeyMap(options);
00187 TQT_DBusMessage reply = driveControl.sendWithReply("Eject", params, &error);
00188 if (error.isValid()) {
00189
00190 printf("[ERROR][tdehwlib] ejectDriveUDisks2: %s\n", error.name().ascii()); fflush(stdout);
00191 return FALSE;
00192 }
00193 else {
00194 return TRUE;
00195 }
00196 }
00197 }
00198 }
00199 }
00200 }
00201 #endif // WITH_UDISKS2
00202 return FALSE;
00203 }
00204
00205 int mountDriveUDisks(TQString deviceNode, TQString fileSystemType, TQStringList mountOptions, TQString* errStr = NULL) {
00206 #ifdef WITH_UDISKS
00207 TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
00208 if (dbusConn.isConnected()) {
00209 TQString blockDeviceString = deviceNode;
00210 blockDeviceString.replace("/dev/", "");
00211 blockDeviceString = "/org/freedesktop/UDisks/devices/" + blockDeviceString;
00212
00213
00214 TQT_DBusError error;
00215 TQT_DBusProxy driveControl("org.freedesktop.UDisks", blockDeviceString, "org.freedesktop.UDisks.Device", dbusConn);
00216 if (driveControl.canSend()) {
00217 TQValueList<TQT_DBusData> params;
00218 params << TQT_DBusData::fromString(fileSystemType);
00219 params << TQT_DBusData::fromList(TQT_DBusDataList(mountOptions));
00220 TQT_DBusMessage reply = driveControl.sendWithReply("FilesystemMount", params, &error);
00221 if (error.isValid()) {
00222
00223 if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") {
00224
00225 return -2;
00226 }
00227 if (errStr) {
00228 *errStr = error.name() + ": " + error.message();
00229 }
00230 else {
00231 printf("[ERROR][tdehwlib] mountDriveUDisks: %s\n", error.name().ascii()); fflush(stdout);
00232 }
00233 return -1;
00234 }
00235 else {
00236 return 0;
00237 }
00238 }
00239 else {
00240 return -2;
00241 }
00242 }
00243 #endif // WITH_UDISKS
00244 return -2;
00245 }
00246
00247 int mountDriveUDisks2(TQString deviceNode, TQString fileSystemType, TQString mountOptions, TQString* errStr = NULL) {
00248 #ifdef WITH_UDISKS2
00249 TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
00250 if (dbusConn.isConnected()) {
00251 TQString blockDeviceString = deviceNode;
00252 blockDeviceString.replace("/dev/", "");
00253 blockDeviceString = "/org/freedesktop/UDisks2/block_devices/" + blockDeviceString;
00254
00255
00256 TQT_DBusError error;
00257 TQT_DBusProxy driveControl("org.freedesktop.UDisks2", blockDeviceString, "org.freedesktop.UDisks2.Filesystem", dbusConn);
00258 if (driveControl.canSend()) {
00259 TQValueList<TQT_DBusData> params;
00260 TQMap<TQString, TQT_DBusData> optionsMap;
00261 if (fileSystemType != "") {
00262 optionsMap["fstype"] = convertDBUSDataToVariantData(TQT_DBusData::fromString(fileSystemType));
00263 }
00264 optionsMap["options"] = convertDBUSDataToVariantData(TQT_DBusData::fromString(mountOptions));
00265 params << TQT_DBusData::fromStringKeyMap(TQT_DBusDataMap<TQString>(optionsMap));
00266 TQT_DBusMessage reply = driveControl.sendWithReply("Mount", params, &error);
00267 if (error.isValid()) {
00268
00269 if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") {
00270
00271 return -2;
00272 }
00273 if (errStr) {
00274 *errStr = error.name() + ": " + error.message();
00275 }
00276 else {
00277 printf("[ERROR][tdehwlib] mountDriveUDisks2: %s\n", error.name().ascii()); fflush(stdout);
00278 }
00279 return -1;
00280 }
00281 else {
00282 return 0;
00283 }
00284 }
00285 else {
00286 return -2;
00287 }
00288 }
00289 #endif // WITH_UDISKS2
00290 return -2;
00291 }
00292
00293 int unMountDriveUDisks(TQString deviceNode, TQStringList unMountOptions, TQString* errStr = NULL) {
00294 #ifdef WITH_UDISKS
00295 TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
00296 if (dbusConn.isConnected()) {
00297 TQString blockDeviceString = deviceNode;
00298 blockDeviceString.replace("/dev/", "");
00299 blockDeviceString = "/org/freedesktop/UDisks/devices/" + blockDeviceString;
00300
00301
00302 TQT_DBusError error;
00303 TQT_DBusProxy driveControl("org.freedesktop.UDisks", blockDeviceString, "org.freedesktop.UDisks.Device", dbusConn);
00304 if (driveControl.canSend()) {
00305 TQValueList<TQT_DBusData> params;
00306 params << TQT_DBusData::fromList(TQT_DBusDataList(unMountOptions));
00307 TQT_DBusMessage reply = driveControl.sendWithReply("FilesystemUnmount", params, &error);
00308 if (error.isValid()) {
00309
00310 if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") {
00311
00312 return -2;
00313 }
00314 if (errStr) {
00315 *errStr = error.name() + ": " + error.message();
00316 }
00317 else {
00318 printf("[ERROR][tdehwlib] unMountDriveUDisks: %s\n", error.name().ascii()); fflush(stdout);
00319 }
00320 return -1;
00321 }
00322 else {
00323 return 0;
00324 }
00325 }
00326 else {
00327 return -2;
00328 }
00329 }
00330 #endif // WITH_UDISKS
00331 return -2;
00332 }
00333
00334 int unMountDriveUDisks2(TQString deviceNode, TQString unMountOptions, TQString* errStr = NULL) {
00335 #ifdef WITH_UDISKS2
00336 TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
00337 if (dbusConn.isConnected()) {
00338 TQString blockDeviceString = deviceNode;
00339 blockDeviceString.replace("/dev/", "");
00340 blockDeviceString = "/org/freedesktop/UDisks2/block_devices/" + blockDeviceString;
00341
00342
00343 TQT_DBusError error;
00344 TQT_DBusProxy driveControl("org.freedesktop.UDisks2", blockDeviceString, "org.freedesktop.UDisks2.Filesystem", dbusConn);
00345 if (driveControl.canSend()) {
00346 TQValueList<TQT_DBusData> params;
00347 TQMap<TQString, TQT_DBusData> optionsMap;
00348 optionsMap["options"] = convertDBUSDataToVariantData(TQT_DBusData::fromString(unMountOptions));
00349 params << TQT_DBusData::fromStringKeyMap(TQT_DBusDataMap<TQString>(optionsMap));
00350 TQT_DBusMessage reply = driveControl.sendWithReply("Unmount", params, &error);
00351 if (error.isValid()) {
00352
00353 if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") {
00354
00355 return -2;
00356 }
00357 if (errStr) {
00358 *errStr = error.name() + ": " + error.message();
00359 }
00360 else {
00361 printf("[ERROR][tdehwlib] unMountDriveUDisks2: %s\n", error.name().ascii()); fflush(stdout);
00362 }
00363 return -1;
00364 }
00365 else {
00366 return 0;
00367 }
00368 }
00369 else {
00370 return -2;
00371 }
00372 }
00373 #endif // WITH_UDISKS2
00374 return -2;
00375 }
00376
00377 bool TDEStorageDevice::ejectDrive() {
00378 #ifdef WITH_UDISKS2
00379 if (!(TDEGlobal::dirs()->findExe("udisksctl").isEmpty())) {
00380 if (ejectDriveUDisks2(this)) {
00381 return TRUE;
00382 }
00383 else {
00384 printf("[tdehwlib] Failed to eject drive '%s' via udisks2, falling back to alternate mechanism\n", deviceNode().ascii());
00385 fflush(stdout);
00386 }
00387 }
00388 #endif // WITH_UDISKS2
00389
00390 #ifdef WITH_UDISKS
00391 if (!(TDEGlobal::dirs()->findExe("udisks").isEmpty())) {
00392 if (ejectDriveUDisks(this)) {
00393 return TRUE;
00394 }
00395 else {
00396 printf("[tdehwlib] Failed to eject drive '%s' via udisks, falling back to alternate mechanism\n", deviceNode().ascii());
00397 fflush(stdout);
00398 }
00399 }
00400 #endif // WITH_UDISKS
00401
00402 if (!(TDEGlobal::dirs()->findExe("eject").isEmpty())) {
00403 TQString command = TQString("eject -v '%1' 2>&1").arg(deviceNode());
00404
00405 FILE *exepipe = popen(command.ascii(), "r");
00406 if (exepipe) {
00407 TQString eject_output;
00408 TQTextStream ts(exepipe, IO_ReadOnly);
00409 eject_output = ts.read();
00410 int retcode = pclose(exepipe);
00411 if (retcode == 0) {
00412 return TRUE;
00413 }
00414 }
00415 printf("[tdehwlib] Failed to eject drive '%s' via 'eject' command\n", deviceNode().ascii());
00416 fflush(stdout);
00417 }
00418
00419 return FALSE;
00420 }
00421
00422 bool TDEStorageDevice::ejectDriveMedia() {
00423 int fd = open(deviceNode().ascii(), O_RDWR | O_NONBLOCK);
00424 if (fd < 0) {
00425 return false;
00426 }
00427 if (ioctl(fd, CDROMEJECT) != 0) {
00428 close(fd);
00429 return false;
00430 }
00431 else {
00432 close(fd);
00433 return true;
00434 }
00435 }
00436
00437 TQString TDEStorageDevice::diskLabel() {
00438 return m_diskName;
00439 }
00440
00441 void TDEStorageDevice::internalSetDiskLabel(TQString dn) {
00442 m_diskName = dn;
00443 }
00444
00445 bool TDEStorageDevice::mediaInserted() {
00446 return m_mediaInserted;
00447 }
00448
00449 void TDEStorageDevice::internalSetMediaInserted(bool inserted) {
00450 m_mediaInserted = inserted;
00451 }
00452
00453 TQString TDEStorageDevice::fileSystemName() {
00454 return m_fileSystemName;
00455 }
00456
00457 void TDEStorageDevice::internalSetFileSystemName(TQString fn) {
00458 m_fileSystemName = fn;
00459 }
00460
00461 TQString TDEStorageDevice::fileSystemUsage() {
00462 return m_fileSystemUsage;
00463 }
00464
00465 void TDEStorageDevice::internalSetFileSystemUsage(TQString fu) {
00466 m_fileSystemUsage = fu;
00467 }
00468
00469 TQString TDEStorageDevice::diskUUID() {
00470 return m_diskUUID;
00471 }
00472
00473 void TDEStorageDevice::internalSetDiskUUID(TQString id) {
00474 m_diskUUID = id;
00475 }
00476
00477 TQStringList TDEStorageDevice::holdingDevices() {
00478 return m_holdingDevices;
00479 }
00480
00481 void TDEStorageDevice::internalSetHoldingDevices(TQStringList hd) {
00482 m_holdingDevices = hd;
00483 }
00484
00485 TQStringList TDEStorageDevice::slaveDevices() {
00486 return m_slaveDevices;
00487 }
00488
00489 void TDEStorageDevice::internalSetSlaveDevices(TQStringList sd) {
00490 m_slaveDevices = sd;
00491 }
00492
00493 TQString decodeHexEncoding(TQString str) {
00494 TQRegExp hexEncRegExp("\\\\x[0-9A-Fa-f]{1,2}");
00495 hexEncRegExp.setMinimal(false);
00496 hexEncRegExp.setCaseSensitive(true);
00497 int s = -1;
00498
00499 while((s = hexEncRegExp.search(str, s+1))>=0){
00500 str.replace(s, hexEncRegExp.cap(0).length(), TQChar((char)strtol(hexEncRegExp.cap(0).mid(2).ascii(), NULL, 16)));
00501 }
00502
00503 return str;
00504 }
00505
00506 TQString TDEStorageDevice::friendlyName() {
00507
00508 TQString devicevendorid = vendorEncoded();
00509 TQString devicemodelid = modelEncoded();
00510
00511 devicevendorid = decodeHexEncoding(devicevendorid);
00512 devicemodelid = decodeHexEncoding(devicemodelid);
00513
00514 devicevendorid = devicevendorid.stripWhiteSpace();
00515 devicemodelid = devicemodelid.stripWhiteSpace();
00516 devicevendorid = devicevendorid.simplifyWhiteSpace();
00517 devicemodelid = devicemodelid.simplifyWhiteSpace();
00518
00519 TQString devicename = devicevendorid + " " + devicemodelid;
00520
00521 devicename = devicename.stripWhiteSpace();
00522 devicename = devicename.simplifyWhiteSpace();
00523
00524 if (devicename != "") {
00525 return devicename;
00526 }
00527
00528 if (isDiskOfType(TDEDiskDeviceType::Camera)) {
00529 return TDEGenericDevice::friendlyName();
00530 }
00531
00532 if (isDiskOfType(TDEDiskDeviceType::Floppy)) {
00533 return friendlyDeviceType();
00534 }
00535
00536 TQString label = diskLabel();
00537 if (label.isNull()) {
00538 if (deviceSize() > 0) {
00539 if (checkDiskStatus(TDEDiskDeviceStatus::Hotpluggable)) {
00540 label = i18n("%1 Removable Device").arg(deviceFriendlySize());
00541 }
00542 else {
00543 label = i18n("%1 Fixed Storage Device").arg(deviceFriendlySize());
00544 }
00545 }
00546 }
00547
00548 if (!label.isNull()) {
00549 return label;
00550 }
00551
00552 return friendlyDeviceType();
00553 }
00554
00555 TQString TDEStorageDevice::detailedFriendlyName() {
00556 return TQString("%1 [%2]").arg(friendlyName()).arg(deviceNode());
00557 }
00558
00559 TQString TDEStorageDevice::friendlyDeviceType() {
00560 TQString ret = i18n("Hard Disk Drive");
00561
00562
00563 if (isDiskOfType(TDEDiskDeviceType::Floppy)) {
00564 ret = i18n("Floppy Drive");
00565 }
00566 if (isDiskOfType(TDEDiskDeviceType::Optical)) {
00567 ret = i18n("Optical Drive");
00568 }
00569 if (isDiskOfType(TDEDiskDeviceType::CDROM)) {
00570 ret = i18n("CDROM Drive");
00571 }
00572 if (isDiskOfType(TDEDiskDeviceType::CDRW)) {
00573 ret = i18n("CDRW Drive");
00574 }
00575 if (isDiskOfType(TDEDiskDeviceType::DVDROM)) {
00576 ret = i18n("DVD Drive");
00577 }
00578 if (isDiskOfType(TDEDiskDeviceType::DVDRW)) {
00579 ret = i18n("DVDRW Drive");
00580 }
00581 if (isDiskOfType(TDEDiskDeviceType::DVDRAM)) {
00582 ret = i18n("DVDRAM Drive");
00583 }
00584 if (isDiskOfType(TDEDiskDeviceType::Zip)) {
00585 ret = i18n("Zip Drive");
00586 }
00587 if (isDiskOfType(TDEDiskDeviceType::Tape)) {
00588 ret = i18n("Tape Drive");
00589 }
00590 if (isDiskOfType(TDEDiskDeviceType::Camera)) {
00591 ret = i18n("Digital Camera");
00592 }
00593
00594 if (isDiskOfType(TDEDiskDeviceType::HDD)) {
00595 ret = i18n("Hard Disk Drive");
00596 if (checkDiskStatus(TDEDiskDeviceStatus::Hotpluggable)) {
00597 ret = i18n("Removable Storage");
00598 }
00599 if (isDiskOfType(TDEDiskDeviceType::CompactFlash)) {
00600 ret = i18n("Compact Flash");
00601 }
00602 if (isDiskOfType(TDEDiskDeviceType::MemoryStick)) {
00603 ret = i18n("Memory Stick");
00604 }
00605 if (isDiskOfType(TDEDiskDeviceType::SmartMedia)) {
00606 ret = i18n("Smart Media");
00607 }
00608 if (isDiskOfType(TDEDiskDeviceType::SDMMC)) {
00609 ret = i18n("Secure Digital");
00610 }
00611 }
00612
00613 if (isDiskOfType(TDEDiskDeviceType::RAM)) {
00614 ret = i18n("Random Access Memory");
00615 }
00616 if (isDiskOfType(TDEDiskDeviceType::Loop)) {
00617 ret = i18n("Loop Device");
00618 }
00619
00620 return ret;
00621 }
00622
00623 TQPixmap TDEStorageDevice::icon(TDEIcon::StdSizes size) {
00624 TQString mountString;
00625 if (mountPath() != TQString::null) {
00626 mountString = "-mounted";
00627 }
00628
00629 TQPixmap ret = DesktopIcon("drive-harddisk" + mountString, size);
00630
00631 if (isDiskOfType(TDEDiskDeviceType::Floppy)) {
00632 ret = DesktopIcon("media-floppy-3_5" + mountString, size);
00633 }
00634 if (isDiskOfType(TDEDiskDeviceType::Optical)) {
00635 ret = DesktopIcon("media-optical-cdrom" + mountString, size);
00636 }
00637 if (isDiskOfType(TDEDiskDeviceType::CDROM)) {
00638 ret = DesktopIcon("media-optical-cdrom" + mountString, size);
00639 }
00640 if (isDiskOfType(TDEDiskDeviceType::CDRW)) {
00641 ret = DesktopIcon("media-optical-cdwriter" + mountString, size);
00642 }
00643 if (isDiskOfType(TDEDiskDeviceType::DVDROM)) {
00644 ret = DesktopIcon("media-optical-dvd" + mountString, size);
00645 }
00646 if (isDiskOfType(TDEDiskDeviceType::DVDRW)) {
00647 ret = DesktopIcon("media-optical-dvd" + mountString, size);
00648 }
00649 if (isDiskOfType(TDEDiskDeviceType::DVDRAM)) {
00650 ret = DesktopIcon("media-optical-dvd" + mountString, size);
00651 }
00652 if (isDiskOfType(TDEDiskDeviceType::Zip)) {
00653 ret = DesktopIcon("media-floppy-zip" + mountString, size);
00654 }
00655 if (isDiskOfType(TDEDiskDeviceType::Tape)) {
00656 ret = DesktopIcon("media-tape" + mountString, size);
00657 }
00658 if (isDiskOfType(TDEDiskDeviceType::Camera)) {
00659 ret = DesktopIcon("camera" + TQString((mountPath() != TQString::null) ? "_mount" : "_umount"), size);
00660 }
00661
00662 if (isDiskOfType(TDEDiskDeviceType::HDD)) {
00663 ret = DesktopIcon("drive-harddisk" + mountString, size);
00664 if (checkDiskStatus(TDEDiskDeviceStatus::Hotpluggable)) {
00665 ret = DesktopIcon("media-flash-usb" + mountString, size);
00666 }
00667 if (isDiskOfType(TDEDiskDeviceType::CompactFlash)) {
00668 ret = DesktopIcon("media-flash-compact_flash" + mountString, size);
00669 }
00670 if (isDiskOfType(TDEDiskDeviceType::MemoryStick)) {
00671 ret = DesktopIcon("media-flash-memory_stick" + mountString, size);
00672 }
00673 if (isDiskOfType(TDEDiskDeviceType::SmartMedia)) {
00674 ret = DesktopIcon("media-flash-smart_media" + mountString, size);
00675 }
00676 if (isDiskOfType(TDEDiskDeviceType::SDMMC)) {
00677 ret = DesktopIcon("media-flash-sd_mmc" + mountString, size);
00678 }
00679 }
00680
00681 if (isDiskOfType(TDEDiskDeviceType::RAM)) {
00682 ret = DesktopIcon("memory" + mountString, size);
00683 }
00684 if (isDiskOfType(TDEDiskDeviceType::Loop)) {
00685 ret = DesktopIcon("blockdevice" + mountString, size);
00686 }
00687
00688 return ret;
00689 }
00690
00691 unsigned long long TDEStorageDevice::deviceSize() {
00692 TQString bsnodename = systemPath();
00693
00694
00695 TQString blocksize = "512";
00696
00697 TQString dsnodename = systemPath();
00698 dsnodename.append("/size");
00699 TQFile dsfile( dsnodename );
00700 TQString devicesize;
00701 if ( dsfile.open( IO_ReadOnly ) ) {
00702 TQTextStream stream( &dsfile );
00703 devicesize = stream.readLine();
00704 dsfile.close();
00705 }
00706
00707 return ((unsigned long long)blocksize.toULong()*(unsigned long long)devicesize.toULong());
00708 }
00709
00710 TQString TDEStorageDevice::deviceFriendlySize() {
00711 return TDEHardwareDevices::bytesToFriendlySizeString(deviceSize());
00712 }
00713
00714 TQString TDEStorageDevice::mountPath() {
00715
00716
00717
00718
00719
00720
00721
00722
00723 TDEGlobal::hardwareDevices()->rescanDeviceInformation(this);
00724
00725 TQString dmnodename = systemPath();
00726 dmnodename.append("/dm/name");
00727 TQFile namefile( dmnodename );
00728 TQString dmaltname;
00729 if ( namefile.open( IO_ReadOnly ) ) {
00730 TQTextStream stream( &namefile );
00731 dmaltname = stream.readLine();
00732 namefile.close();
00733 }
00734 if (!dmaltname.isNull()) {
00735 dmaltname.prepend("/dev/mapper/");
00736 }
00737
00738 TQStringList lines;
00739 TQFile file( "/proc/mounts" );
00740 if ( file.open( IO_ReadOnly ) ) {
00741 TQTextStream stream( &file );
00742 TQString line;
00743 while ( !stream.atEnd() ) {
00744 line = stream.readLine();
00745 TQStringList mountInfo = TQStringList::split(" ", line, true);
00746 TQString testNode = *mountInfo.at(0);
00747
00748 if ((testNode == deviceNode()) || (testNode == dmaltname) || (testNode == ("/dev/disk/by-uuid/" + diskUUID()))) {
00749 TQString ret = *mountInfo.at(1);
00750 ret.replace("\\040", " ");
00751 return ret;
00752 }
00753 lines += line;
00754 }
00755 file.close();
00756 }
00757
00758
00759
00760 TQStringList slaveDeviceList = holdingDevices();
00761 for ( TQStringList::Iterator slavedevit = slaveDeviceList.begin(); slavedevit != slaveDeviceList.end(); ++slavedevit ) {
00762
00763 TDEHardwareDevices *hwdevices = TDEGlobal::hardwareDevices();
00764 TDEGenericDevice *hwdevice = hwdevices->findBySystemPath(*slavedevit);
00765 if ((hwdevice) && (hwdevice->type() == TDEGenericDeviceType::Disk)) {
00766 TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(hwdevice);
00767 return sdevice->mountPath();
00768 }
00769 }
00770
00771 return TQString::null;
00772 }
00773
00774 TQString TDEStorageDevice::mountDevice(TQString mediaName, TDEStorageMountOptions mountOptions, TQString* errRet, int* retcode) {
00775 int internal_retcode;
00776 if (!retcode) {
00777 retcode = &internal_retcode;
00778 }
00779
00780 TQString ret = mountPath();
00781
00782
00783 if (!ret.isNull()) {
00784 return ret;
00785 }
00786
00787 TQString command;
00788 TQString devNode = deviceNode();
00789 devNode.replace("'", "'\\''");
00790 mediaName.replace("'", "'\\''");
00791
00792 #if defined(WITH_UDISKS2) || defined(WITH_UDISKS)
00793
00794 TQStringList udisksOptions;
00795 TQString optionString;
00796
00797 if (mountOptions["ro"] == "true") {
00798 udisksOptions.append("ro");
00799 }
00800
00801 if (mountOptions["atime"] != "true") {
00802 udisksOptions.append("noatime");
00803 }
00804
00805 if (mountOptions["sync"] == "true") {
00806 udisksOptions.append("sync");
00807 }
00808
00809 if( (mountOptions["filesystem"] == "fat")
00810 || (mountOptions["filesystem"] == "vfat")
00811 || (mountOptions["filesystem"] == "msdos")
00812 || (mountOptions["filesystem"] == "umsdos")
00813 ) {
00814 if (mountOptions.contains("shortname")) {
00815 udisksOptions.append(TQString("shortname=%1").arg(mountOptions["shortname"]));
00816 }
00817 }
00818
00819 if( (mountOptions["filesystem"] == "jfs")) {
00820 if (mountOptions["utf8"] == "true") {
00821
00822
00823 }
00824 }
00825
00826 if( (mountOptions["filesystem"] == "ntfs-3g") ) {
00827 if (mountOptions.contains("locale")) {
00828 udisksOptions.append(TQString("locale=%1").arg(mountOptions["locale"]));
00829 }
00830 }
00831
00832 if( (mountOptions["filesystem"] == "ext3")
00833 || (mountOptions["filesystem"] == "ext4")
00834 ) {
00835 if (mountOptions.contains("journaling")) {
00836
00837
00838 }
00839 }
00840
00841 for (TQStringList::Iterator it = udisksOptions.begin(); it != udisksOptions.end(); ++it) {
00842 optionString.append(",");
00843 optionString.append(*it);
00844 }
00845
00846 if (!optionString.isEmpty()) {
00847 optionString.remove(0, 1);
00848 }
00849 #endif // defined(WITH_UDISKS2) || defined(WITH_UDISKS)
00850
00851 #ifdef WITH_UDISKS2
00852 if(command.isEmpty()) {
00853
00854 TQString errorString;
00855 TQString fileSystemType;
00856
00857 if (mountOptions.contains("filesystem") && !mountOptions["filesystem"].isEmpty()) {
00858 fileSystemType = mountOptions["filesystem"];
00859 }
00860
00861 int uDisks2Ret = mountDriveUDisks2(devNode, fileSystemType, optionString, &errorString);
00862 if (uDisks2Ret == 0) {
00863
00864 TDEGlobal::hardwareDevices()->processModifiedMounts();
00865
00866 ret = mountPath();
00867 return ret;
00868 }
00869 else if (uDisks2Ret == -1) {
00870 if (errRet) {
00871 *errRet = errorString;
00872 }
00873
00874
00875 TDEGlobal::hardwareDevices()->processModifiedMounts();
00876
00877 ret = mountPath();
00878 return ret;
00879 }
00880 else {
00881
00882 command = TQString::null;
00883 }
00884 }
00885 #endif // WITH_UDISKS2
00886
00887 #ifdef WITH_UDISKS
00888 if(command.isEmpty()) {
00889
00890 TQString errorString;
00891 TQString fileSystemType;
00892
00893 if (mountOptions.contains("filesystem") && !mountOptions["filesystem"].isEmpty()) {
00894 fileSystemType = mountOptions["filesystem"];
00895 }
00896
00897 int uDisksRet = mountDriveUDisks(devNode, fileSystemType, udisksOptions, &errorString);
00898 if (uDisksRet == 0) {
00899
00900 TDEGlobal::hardwareDevices()->processModifiedMounts();
00901
00902 ret = mountPath();
00903 return ret;
00904 }
00905 else if (uDisksRet == -1) {
00906 if (errRet) {
00907 *errRet = errorString;
00908 }
00909
00910
00911 TDEGlobal::hardwareDevices()->processModifiedMounts();
00912
00913 ret = mountPath();
00914 return ret;
00915 }
00916 else {
00917
00918 command = TQString::null;
00919 }
00920 }
00921 #endif // WITH_UDISKS
00922
00923 if(command.isEmpty()) {
00924
00925 TQString pmountProg = TDEGlobal::dirs()->findExe("pmount");
00926 if (!pmountProg.isEmpty()) {
00927
00928 KTempFile passwordFile(TQString::null, "tmp", 0600);
00929 passwordFile.setAutoDelete(true);
00930
00931 TQString optionString;
00932 if (mountOptions["ro"] == "true") {
00933 optionString.append(" -r");
00934 }
00935
00936 if (mountOptions["atime"] != "true") {
00937 optionString.append(" -A");
00938 }
00939
00940 if (mountOptions["utf8"] == "true") {
00941 optionString.append(" -c utf8");
00942 }
00943
00944 if (mountOptions["sync"] == "true") {
00945 optionString.append(" -s");
00946 }
00947
00948 if (mountOptions.contains("filesystem") && !mountOptions["filesystem"].isEmpty()) {
00949 optionString.append(TQString(" -t %1").arg(mountOptions["filesystem"]));
00950 }
00951
00952 if (mountOptions.contains("locale")) {
00953 optionString.append(TQString(" -c %1").arg(mountOptions["locale"]));
00954 }
00955
00956 TQString mountpoint;
00957 if (mountOptions.contains("mountpoint")
00958 && !mountOptions["mountpoint"].isEmpty()
00959 && (mountOptions["mountpoint"] != "/media/")) {
00960 mountpoint = mountOptions["mountpoint"];
00961 mountpoint.replace("'", "'\\''");
00962 }
00963 else {
00964 mountpoint = mediaName;
00965 }
00966
00967 TQString passFileName = passwordFile.name();
00968 passFileName.replace("'", "'\\''");
00969
00970 command = TQString("pmount -p '%1' %2 '%3' '%4' 2>&1").arg(passFileName).arg(optionString).arg(devNode).arg(mountpoint);
00971 }
00972 }
00973
00974 if(command.isEmpty()) {
00975 if (errRet) {
00976 *errRet = i18n("No supported mounting methods were detected on your system");
00977 }
00978 return ret;
00979 }
00980
00981 FILE *exepipe = popen(command.local8Bit(), "r");
00982 if (exepipe) {
00983 TQString mount_output;
00984 TQTextStream* ts = new TQTextStream(exepipe, IO_ReadOnly);
00985 mount_output = ts->read();
00986 delete ts;
00987 *retcode = pclose(exepipe);
00988 if (errRet) {
00989 *errRet = mount_output;
00990 }
00991 }
00992
00993
00994 TDEGlobal::hardwareDevices()->processModifiedMounts();
00995
00996 ret = mountPath();
00997
00998 return ret;
00999 }
01000
01001 TQString TDEStorageDevice::mountEncryptedDevice(TQString passphrase, TQString mediaName, TDEStorageMountOptions mountOptions, TQString* errRet, int* retcode) {
01002 int internal_retcode;
01003 if (!retcode) {
01004 retcode = &internal_retcode;
01005 }
01006
01007 TQString ret = mountPath();
01008
01009 if (!ret.isNull()) {
01010 return ret;
01011 }
01012
01013
01014 KTempFile passwordFile(TQString::null, "tmp", 0600);
01015 passwordFile.setAutoDelete(true);
01016 TQFile* pwFile = passwordFile.file();
01017 if (!pwFile) {
01018 return TQString::null;
01019 }
01020
01021 pwFile->writeBlock(passphrase.ascii(), passphrase.length());
01022 pwFile->flush();
01023
01024 TQString optionString;
01025 if (mountOptions["ro"] == "true") {
01026 optionString.append(" -r");
01027 }
01028
01029 if (mountOptions["atime"] != "true") {
01030 optionString.append(" -A");
01031 }
01032
01033 if (mountOptions["utf8"] == "true") {
01034 optionString.append(" -c utf8");
01035 }
01036
01037 if (mountOptions["sync"] == "true") {
01038 optionString.append(" -s");
01039 }
01040
01041 if (mountOptions.contains("filesystem") && !mountOptions["filesystem"].isEmpty()) {
01042 optionString.append(TQString(" -t %1").arg(mountOptions["filesystem"]));
01043 }
01044
01045 if (mountOptions.contains("locale")) {
01046 optionString.append(TQString(" -c %1").arg(mountOptions["locale"]));
01047 }
01048
01049 TQString passFileName = passwordFile.name();
01050 TQString devNode = deviceNode();
01051 passFileName.replace("'", "'\\''");
01052 devNode.replace("'", "'\\''");
01053 mediaName.replace("'", "'\\''");
01054 TQString command = TQString("pmount -p '%1' %2 '%3' '%4' 2>&1").arg(passFileName).arg(optionString).arg(devNode).arg(mediaName);
01055
01056 FILE *exepipe = popen(command.local8Bit(), "r");
01057 if (exepipe) {
01058 TQString mount_output;
01059 TQTextStream* ts = new TQTextStream(exepipe, IO_ReadOnly);
01060 mount_output = ts->read();
01061 delete ts;
01062 *retcode = pclose(exepipe);
01063 if (errRet) {
01064 *errRet = mount_output;
01065 }
01066 }
01067
01068
01069 TDEGlobal::hardwareDevices()->processModifiedMounts();
01070
01071 ret = mountPath();
01072
01073 return ret;
01074 }
01075
01076 bool TDEStorageDevice::unmountDevice(TQString* errRet, int* retcode) {
01077 int internal_retcode;
01078 if (!retcode) {
01079 retcode = &internal_retcode;
01080 }
01081
01082 TQString mountpoint = mountPath();
01083 TQString devNode = deviceNode();
01084
01085 if (mountpoint.isNull()) {
01086 return true;
01087 }
01088
01089 mountpoint.replace("'", "'\\''");
01090
01091 TQString command;
01092
01093 #ifdef WITH_UDISKS2
01094 if(command.isEmpty()) {
01095
01096 TQString errorString;
01097 int unMountUDisks2Ret = unMountDriveUDisks2(devNode, TQString::null, &errorString);
01098 if (unMountUDisks2Ret == 0) {
01099
01100 TDEGlobal::hardwareDevices()->processModifiedMounts();
01101
01102 return true;
01103 }
01104 else if (unMountUDisks2Ret == -1) {
01105 if (errRet) {
01106 *errRet = errorString;
01107 }
01108
01109
01110 TDEGlobal::hardwareDevices()->processModifiedMounts();
01111
01112 return false;
01113 }
01114 else {
01115
01116 command = TQString::null;
01117 }
01118 }
01119 #endif // WITH_UDISKS2
01120 #ifdef WITH_UDISKS
01121 if(command.isEmpty()) {
01122
01123 TQString errorString;
01124 int unMountUDisksRet = unMountDriveUDisks(devNode, TQStringList(), &errorString);
01125 if (unMountUDisksRet == 0) {
01126
01127 TDEGlobal::hardwareDevices()->processModifiedMounts();
01128
01129 return true;
01130 }
01131 else if (unMountUDisksRet == -1) {
01132 if (errRet) {
01133 *errRet = errorString;
01134 }
01135
01136
01137 TDEGlobal::hardwareDevices()->processModifiedMounts();
01138
01139 return false;
01140 }
01141 else {
01142
01143 command = TQString::null;
01144 }
01145 }
01146 #endif // WITH_UDISKS
01147 if(command.isEmpty() &&
01148 !(TDEGlobal::dirs()->findExe("pumount").isEmpty())) {
01149 command = TQString("pumount '%1' 2>&1").arg(mountpoint);
01150 }
01151
01152 if(command.isEmpty()) {
01153 if (errRet) {
01154 *errRet = i18n("No supported unmounting methods were detected on your system");
01155 }
01156 return true;
01157 }
01158
01159 FILE *exepipe = popen(command.local8Bit(), "r");
01160 if (exepipe) {
01161 TQString umount_output;
01162 TQTextStream* ts = new TQTextStream(exepipe, IO_ReadOnly);
01163 umount_output = ts->read();
01164 delete ts;
01165 *retcode = pclose(exepipe);
01166 if (*retcode == 0) {
01167
01168 TDEGlobal::hardwareDevices()->processModifiedMounts();
01169
01170 return true;
01171 }
01172 else {
01173 if (errRet) {
01174 *errRet = umount_output;
01175 }
01176 }
01177 }
01178
01179
01180 TDEGlobal::hardwareDevices()->processModifiedMounts();
01181
01182 return false;
01183 }
01184
01185 TQString TDEStorageDevice::determineFileSystemType(TQString path) {
01186 TQStringList mountTable;
01187 TQString prevPath = path;
01188 dev_t prevDev = 0;
01189 int pos;
01190 struct stat directory_info;
01191 if (path.startsWith("/")) {
01192 stat(path.local8Bit(), &directory_info);
01193 prevDev = directory_info.st_dev;
01194
01195
01196 while (path != "/") {
01197 pos = path.findRev("/", -1, TRUE);
01198 if (pos < 0) {
01199 break;
01200 }
01201 path = path.mid(0, pos);
01202 if (path == "") {
01203 path = "/";
01204 }
01205 stat(path.local8Bit(), &directory_info);
01206 if (directory_info.st_dev != prevDev) {
01207 break;
01208 }
01209 prevPath = path;
01210 prevDev = directory_info.st_dev;
01211 }
01212 }
01213
01214
01215 mountTable.clear();
01216 TQFile file( "/proc/mounts" );
01217 if ( file.open( IO_ReadOnly ) ) {
01218 TQTextStream stream( &file );
01219 while ( !stream.atEnd() ) {
01220 mountTable.append(stream.readLine());
01221 }
01222 file.close();
01223 }
01224
01225
01226 TQStringList::Iterator it;
01227 for ( it = mountTable.begin(); it != mountTable.end(); ++it ) {
01228 TQStringList mountInfo = TQStringList::split(" ", (*it), true);
01229 if ((*mountInfo.at(1)) == prevPath) {
01230 return (*mountInfo.at(2));
01231 }
01232 }
01233
01234
01235 return TQString::null;
01236 }
01237
01238 #include "tdestoragedevice.moc"