20 #include "tdestoragedevice.h"
25 #include <sys/ioctl.h>
26 #include <linux/cdrom.h>
33 #include "tdeglobal.h"
34 #include "kiconloader.h"
35 #include "tdetempfile.h"
36 #include "kstandarddirs.h"
38 #include "tdehardwaredevices.h"
43 #if defined(WITH_UDISKS) || defined(WITH_UDISKS2)
44 #include <tqdbusdata.h>
45 #include <tqdbusmessage.h>
46 #include <tqdbusproxy.h>
47 #include <tqdbusvariant.h>
48 #include <tqdbusconnection.h>
49 #include <tqdbuserror.h>
50 #include <tqdbusdatamap.h>
51 #include <tqdbusobjectpath.h>
52 #endif // defined(WITH_UDISKS) || defined(WITH_UDISKS2)
53 #if defined(WITH_UDISKS)
54 #include "tqdbusdatalist.h"
55 #endif // ddefined(WITH_UDISKS)
57 #if defined(WITH_UDISKS) || defined(WITH_UDISKS2)
59 TQT_DBusData convertDBUSDataToVariantData(TQT_DBusData);
60 #endif // defined(WITH_UDISKS) || defined(WITH_UDISKS2)
62 TDEStorageDevice::TDEStorageDevice(TDEGenericDeviceType::TDEGenericDeviceType dt, TQString dn) : TDEGenericDevice(dt, dn), m_mediaInserted(true) {
63 m_diskType = TDEDiskDeviceType::Null;
64 m_diskStatus = TDEDiskDeviceStatus::Null;
67 TDEStorageDevice::~TDEStorageDevice() {
70 TDEDiskDeviceType::TDEDiskDeviceType TDEStorageDevice::diskType() {
74 void TDEStorageDevice::internalSetDiskType(TDEDiskDeviceType::TDEDiskDeviceType dt) {
78 bool TDEStorageDevice::isDiskOfType(TDEDiskDeviceType::TDEDiskDeviceType tf) {
79 return ((m_diskType&tf)!=TDEDiskDeviceType::Null);
82 TDEDiskDeviceStatus::TDEDiskDeviceStatus TDEStorageDevice::diskStatus() {
86 void TDEStorageDevice::internalSetDiskStatus(TDEDiskDeviceStatus::TDEDiskDeviceStatus st) {
90 bool TDEStorageDevice::checkDiskStatus(TDEDiskDeviceStatus::TDEDiskDeviceStatus sf) {
91 return ((m_diskStatus&sf)!=(TDEDiskDeviceStatus::TDEDiskDeviceStatus)0);
94 bool TDEStorageDevice::lockDriveMedia(
bool lock) {
95 int fd =
open(deviceNode().ascii(), O_RDWR | O_NONBLOCK);
99 if (ioctl(fd, CDROM_LOCKDOOR, (lock)?1:0) != 0) {
109 bool ejectDriveUDisks(TDEStorageDevice* sdevice) {
111 TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
112 if (dbusConn.isConnected()) {
113 TQString blockDeviceString = sdevice->deviceNode();
114 blockDeviceString.replace(
"/dev/",
"");
115 blockDeviceString.replace(
"-",
"_2d");
116 blockDeviceString =
"/org/freedesktop/UDisks/devices/" + blockDeviceString;
120 TQT_DBusProxy driveControl(
"org.freedesktop.UDisks", blockDeviceString,
"org.freedesktop.UDisks.Device", dbusConn);
121 if (driveControl.canSend()) {
122 TQValueList<TQT_DBusData> params;
123 TQT_DBusDataList options;
124 params << TQT_DBusData::fromList(options);
125 TQT_DBusMessage reply = driveControl.sendWithReply(
"DriveEject", params, &error);
126 if (error.isValid()) {
128 printf(
"[ERROR][tdehwlib] ejectDriveUDisks: %s\n", error.name().ascii()); fflush(stdout);
136 #endif // WITH_UDISKS
140 bool ejectDriveUDisks2(TDEStorageDevice* sdevice) {
142 TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
143 if (dbusConn.isConnected()) {
144 TQString blockDeviceString = sdevice->deviceNode();
145 blockDeviceString.replace(
"/dev/",
"");
146 blockDeviceString.replace(
"-",
"_2d");
147 blockDeviceString =
"/org/freedesktop/UDisks2/block_devices/" + blockDeviceString;
148 TQT_DBusProxy hardwareControl(
"org.freedesktop.UDisks2", blockDeviceString,
"org.freedesktop.DBus.Properties", dbusConn);
149 if (hardwareControl.canSend()) {
152 TQValueList<TQT_DBusData> params;
153 params << TQT_DBusData::fromString(
"org.freedesktop.UDisks2.Block") << TQT_DBusData::fromString(
"Drive");
154 TQT_DBusMessage reply = hardwareControl.sendWithReply(
"Get", params, &error);
155 if (error.isValid()) {
157 printf(
"[ERROR][tdehwlib] ejectDriveUDisks2: %s\n", error.name().ascii()); fflush(stdout);
161 if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
162 TQT_DBusObjectPath driveObjectPath = reply[0].toVariant().value.toObjectPath();
163 if (!driveObjectPath.isValid()) {
167 error = TQT_DBusError();
168 TQT_DBusProxy driveInformation(
"org.freedesktop.UDisks2", driveObjectPath,
"org.freedesktop.DBus.Properties", dbusConn);
170 TQValueList<TQT_DBusData> params;
171 params << TQT_DBusData::fromString(
"org.freedesktop.UDisks2.Drive") << TQT_DBusData::fromString(
"Ejectable");
172 TQT_DBusMessage reply = driveInformation.sendWithReply(
"Get", params, &error);
173 if (error.isValid()) {
175 printf(
"[ERROR][tdehwlib] ejectDriveUDisks2: %s\n", error.name().ascii()); fflush(stdout);
178 if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
179 bool ejectable = reply[0].toVariant().value.toBool();
185 TQT_DBusProxy driveControl(
"org.freedesktop.UDisks2", driveObjectPath,
"org.freedesktop.UDisks2.Drive", dbusConn);
186 TQValueList<TQT_DBusData> params;
187 TQT_DBusDataMap<TQString> options(TQT_DBusData::Variant);
188 params << TQT_DBusData::fromStringKeyMap(options);
189 TQT_DBusMessage reply = driveControl.sendWithReply(
"Eject", params, &error);
190 if (error.isValid()) {
192 printf(
"[ERROR][tdehwlib] ejectDriveUDisks2: %s\n", error.name().ascii()); fflush(stdout);
203 #endif // WITH_UDISKS2
207 int mountDriveUDisks(TQString deviceNode, TQString fileSystemType, TQStringList mountOptions, TQString* errStr = NULL) {
209 TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
210 if (dbusConn.isConnected()) {
211 TQString blockDeviceString = deviceNode;
212 blockDeviceString.replace(
"/dev/",
"");
213 blockDeviceString.replace(
"-",
"_2d");
214 blockDeviceString =
"/org/freedesktop/UDisks/devices/" + blockDeviceString;
218 TQT_DBusProxy driveControl(
"org.freedesktop.UDisks", blockDeviceString,
"org.freedesktop.UDisks.Device", dbusConn);
219 if (driveControl.canSend()) {
220 TQValueList<TQT_DBusData> params;
221 params << TQT_DBusData::fromString(fileSystemType);
222 params << TQT_DBusData::fromList(TQT_DBusDataList(mountOptions));
223 TQT_DBusMessage reply = driveControl.sendWithReply(
"FilesystemMount", params, &error);
224 if (error.isValid()) {
226 if (error.name() ==
"org.freedesktop.DBus.Error.ServiceUnknown") {
231 *errStr = error.name() +
": " + error.message();
234 printf(
"[ERROR][tdehwlib] mountDriveUDisks: %s\n", error.name().ascii()); fflush(stdout);
246 #endif // WITH_UDISKS
250 int mountDriveUDisks2(TQString deviceNode, TQString fileSystemType, TQString mountOptions, TQString* errStr = NULL) {
252 TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
253 if (dbusConn.isConnected()) {
254 TQString blockDeviceString = deviceNode;
255 blockDeviceString.replace(
"/dev/",
"");
256 blockDeviceString.replace(
"-",
"_2d");
257 blockDeviceString =
"/org/freedesktop/UDisks2/block_devices/" + blockDeviceString;
261 TQT_DBusProxy driveControl(
"org.freedesktop.UDisks2", blockDeviceString,
"org.freedesktop.UDisks2.Filesystem", dbusConn);
262 if (driveControl.canSend()) {
263 TQValueList<TQT_DBusData> params;
264 TQMap<TQString, TQT_DBusData> optionsMap;
265 if (fileSystemType !=
"") {
266 optionsMap[
"fstype"] = convertDBUSDataToVariantData(TQT_DBusData::fromString(fileSystemType));
268 optionsMap[
"options"] = convertDBUSDataToVariantData(TQT_DBusData::fromString(mountOptions));
269 params << TQT_DBusData::fromStringKeyMap(TQT_DBusDataMap<TQString>(optionsMap));
270 TQT_DBusMessage reply = driveControl.sendWithReply(
"Mount", params, &error);
271 if (error.isValid()) {
273 if (error.name() ==
"org.freedesktop.DBus.Error.ServiceUnknown") {
278 *errStr = error.name() +
": " + error.message();
281 printf(
"[ERROR][tdehwlib] mountDriveUDisks2: %s\n", error.name().ascii()); fflush(stdout);
293 #endif // WITH_UDISKS2
297 int unMountDriveUDisks(TQString deviceNode, TQStringList unMountOptions, TQString* errStr = NULL) {
299 TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
300 if (dbusConn.isConnected()) {
301 TQString blockDeviceString = deviceNode;
302 blockDeviceString.replace(
"/dev/",
"");
303 blockDeviceString.replace(
"-",
"_2d");
304 blockDeviceString =
"/org/freedesktop/UDisks/devices/" + blockDeviceString;
308 TQT_DBusProxy driveControl(
"org.freedesktop.UDisks", blockDeviceString,
"org.freedesktop.UDisks.Device", dbusConn);
309 if (driveControl.canSend()) {
310 TQValueList<TQT_DBusData> params;
311 params << TQT_DBusData::fromList(TQT_DBusDataList(unMountOptions));
312 TQT_DBusMessage reply = driveControl.sendWithReply(
"FilesystemUnmount", params, &error);
313 if (error.isValid()) {
315 if (error.name() ==
"org.freedesktop.DBus.Error.ServiceUnknown") {
320 *errStr = error.name() +
": " + error.message();
323 printf(
"[ERROR][tdehwlib] unMountDriveUDisks: %s\n", error.name().ascii()); fflush(stdout);
335 #endif // WITH_UDISKS
339 int unMountDriveUDisks2(TQString deviceNode, TQString unMountOptions, TQString* errStr = NULL) {
341 TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
342 if (dbusConn.isConnected()) {
343 TQString blockDeviceString = deviceNode;
344 blockDeviceString.replace(
"/dev/",
"");
345 blockDeviceString.replace(
"-",
"_2d");
346 blockDeviceString =
"/org/freedesktop/UDisks2/block_devices/" + blockDeviceString;
350 TQT_DBusProxy driveControl(
"org.freedesktop.UDisks2", blockDeviceString,
"org.freedesktop.UDisks2.Filesystem", dbusConn);
351 if (driveControl.canSend()) {
352 TQValueList<TQT_DBusData> params;
353 TQMap<TQString, TQT_DBusData> optionsMap;
354 optionsMap[
"options"] = convertDBUSDataToVariantData(TQT_DBusData::fromString(unMountOptions));
355 params << TQT_DBusData::fromStringKeyMap(TQT_DBusDataMap<TQString>(optionsMap));
356 TQT_DBusMessage reply = driveControl.sendWithReply(
"Unmount", params, &error);
357 if (error.isValid()) {
359 if (error.name() ==
"org.freedesktop.DBus.Error.ServiceUnknown") {
364 *errStr = error.name() +
": " + error.message();
367 printf(
"[ERROR][tdehwlib] unMountDriveUDisks2: %s\n", error.name().ascii()); fflush(stdout);
379 #endif // WITH_UDISKS2
383 bool TDEStorageDevice::ejectDrive() {
386 if (ejectDriveUDisks2(
this)) {
390 printf(
"[tdehwlib] Failed to eject drive '%s' via udisks2, falling back to alternate mechanism\n", deviceNode().ascii());
394 #endif // WITH_UDISKS2
398 if (ejectDriveUDisks(
this)) {
402 printf(
"[tdehwlib] Failed to eject drive '%s' via udisks, falling back to alternate mechanism\n", deviceNode().ascii());
406 #endif // WITH_UDISKS
409 TQString command = TQString(
"eject -v '%1' 2>&1").arg(deviceNode());
411 FILE *exepipe = popen(command.ascii(),
"r");
413 TQString eject_output;
414 TQTextStream ts(exepipe, IO_ReadOnly);
415 eject_output = ts.read();
416 int retcode = pclose(exepipe);
421 printf(
"[tdehwlib] Failed to eject drive '%s' via 'eject' command\n", deviceNode().ascii());
428 bool TDEStorageDevice::ejectDriveMedia() {
429 int fd =
open(deviceNode().ascii(), O_RDWR | O_NONBLOCK);
433 if (ioctl(fd, CDROMEJECT) != 0) {
443 TQString TDEStorageDevice::diskLabel() {
447 void TDEStorageDevice::internalSetDiskLabel(TQString dn) {
451 bool TDEStorageDevice::mediaInserted() {
452 return m_mediaInserted;
455 void TDEStorageDevice::internalSetMediaInserted(
bool inserted) {
456 m_mediaInserted = inserted;
459 TQString TDEStorageDevice::fileSystemName() {
460 return m_fileSystemName;
463 void TDEStorageDevice::internalSetFileSystemName(TQString fn) {
464 m_fileSystemName = fn;
467 TQString TDEStorageDevice::fileSystemUsage() {
468 return m_fileSystemUsage;
471 void TDEStorageDevice::internalSetFileSystemUsage(TQString fu) {
472 m_fileSystemUsage = fu;
475 TQString TDEStorageDevice::diskUUID() {
479 void TDEStorageDevice::internalSetDiskUUID(TQString
id) {
483 TQStringList TDEStorageDevice::holdingDevices() {
484 return m_holdingDevices;
487 void TDEStorageDevice::internalSetHoldingDevices(TQStringList hd) {
488 m_holdingDevices = hd;
491 TQStringList TDEStorageDevice::slaveDevices() {
492 return m_slaveDevices;
495 void TDEStorageDevice::internalSetSlaveDevices(TQStringList sd) {
499 TQString decodeHexEncoding(TQString str) {
500 TQRegExp hexEncRegExp(
"\\\\x[0-9A-Fa-f]{1,2}");
501 hexEncRegExp.setMinimal(
false);
502 hexEncRegExp.setCaseSensitive(
true);
505 while((s = hexEncRegExp.search(str, s+1))>=0){
506 str.replace(s, hexEncRegExp.cap(0).length(), TQChar((
char)strtol(hexEncRegExp.cap(0).mid(2).ascii(), NULL, 16)));
512 TQString TDEStorageDevice::friendlyName() {
514 TQString devicevendorid = vendorEncoded();
515 TQString devicemodelid = modelEncoded();
517 devicevendorid = decodeHexEncoding(devicevendorid);
518 devicemodelid = decodeHexEncoding(devicemodelid);
520 devicevendorid = devicevendorid.stripWhiteSpace();
521 devicemodelid = devicemodelid.stripWhiteSpace();
522 devicevendorid = devicevendorid.simplifyWhiteSpace();
523 devicemodelid = devicemodelid.simplifyWhiteSpace();
525 TQString devicename = devicevendorid +
" " + devicemodelid;
527 devicename = devicename.stripWhiteSpace();
528 devicename = devicename.simplifyWhiteSpace();
530 if (devicename !=
"") {
534 if (isDiskOfType(TDEDiskDeviceType::Camera)) {
535 return TDEGenericDevice::friendlyName();
538 if (isDiskOfType(TDEDiskDeviceType::Floppy)) {
539 return friendlyDeviceType();
542 TQString
label = diskLabel();
543 if (label.isNull()) {
544 if (deviceSize() > 0) {
545 if (checkDiskStatus(TDEDiskDeviceStatus::Hotpluggable)) {
546 label =
i18n(
"%1 Removable Device").arg(deviceFriendlySize());
549 label =
i18n(
"%1 Fixed Storage Device").arg(deviceFriendlySize());
554 if (!label.isNull()) {
558 return friendlyDeviceType();
561 TQString TDEStorageDevice::detailedFriendlyName() {
562 return TQString(
"%1 [%2]").arg(friendlyName()).arg(deviceNode());
565 TQString TDEStorageDevice::friendlyDeviceType() {
566 TQString ret =
i18n(
"Hard Disk Drive");
569 if (isDiskOfType(TDEDiskDeviceType::Floppy)) {
570 ret =
i18n(
"Floppy Drive");
572 if (isDiskOfType(TDEDiskDeviceType::Optical)) {
573 ret =
i18n(
"Optical Drive");
575 if (isDiskOfType(TDEDiskDeviceType::CDROM)) {
576 ret =
i18n(
"CDROM Drive");
578 if (isDiskOfType(TDEDiskDeviceType::CDRW)) {
579 ret =
i18n(
"CDRW Drive");
581 if (isDiskOfType(TDEDiskDeviceType::DVDROM)) {
582 ret =
i18n(
"DVD Drive");
584 if (isDiskOfType(TDEDiskDeviceType::DVDRW)) {
585 ret =
i18n(
"DVDRW Drive");
587 if (isDiskOfType(TDEDiskDeviceType::DVDRAM)) {
588 ret =
i18n(
"DVDRAM Drive");
590 if (isDiskOfType(TDEDiskDeviceType::Zip)) {
591 ret =
i18n(
"Zip Drive");
593 if (isDiskOfType(TDEDiskDeviceType::Tape)) {
594 ret =
i18n(
"Tape Drive");
596 if (isDiskOfType(TDEDiskDeviceType::Camera)) {
597 ret =
i18n(
"Digital Camera");
600 if (isDiskOfType(TDEDiskDeviceType::HDD)) {
601 ret =
i18n(
"Hard Disk Drive");
602 if (checkDiskStatus(TDEDiskDeviceStatus::Hotpluggable)) {
603 ret =
i18n(
"Removable Storage");
605 if (isDiskOfType(TDEDiskDeviceType::CompactFlash)) {
606 ret =
i18n(
"Compact Flash");
608 if (isDiskOfType(TDEDiskDeviceType::MemoryStick)) {
609 ret =
i18n(
"Memory Stick");
611 if (isDiskOfType(TDEDiskDeviceType::SmartMedia)) {
612 ret =
i18n(
"Smart Media");
614 if (isDiskOfType(TDEDiskDeviceType::SDMMC)) {
615 ret =
i18n(
"Secure Digital");
619 if (isDiskOfType(TDEDiskDeviceType::RAM)) {
620 ret =
i18n(
"Random Access Memory");
622 if (isDiskOfType(TDEDiskDeviceType::Loop)) {
623 ret =
i18n(
"Loop Device");
630 TQString mountString;
631 if (mountPath() != TQString::null) {
632 mountString =
"-mounted";
635 TQPixmap ret =
DesktopIcon(
"drive-harddisk" + mountString, size);
637 if (isDiskOfType(TDEDiskDeviceType::Floppy)) {
638 ret =
DesktopIcon(
"media-floppy-3_5" + mountString, size);
640 if (isDiskOfType(TDEDiskDeviceType::Optical)) {
641 ret =
DesktopIcon(
"media-optical-cdrom" + mountString, size);
643 if (isDiskOfType(TDEDiskDeviceType::CDROM)) {
644 ret =
DesktopIcon(
"media-optical-cdrom" + mountString, size);
646 if (isDiskOfType(TDEDiskDeviceType::CDRW)) {
647 ret =
DesktopIcon(
"media-optical-cdwriter" + mountString, size);
649 if (isDiskOfType(TDEDiskDeviceType::DVDROM)) {
650 ret =
DesktopIcon(
"media-optical-dvd" + mountString, size);
652 if (isDiskOfType(TDEDiskDeviceType::DVDRW)) {
653 ret =
DesktopIcon(
"media-optical-dvd" + mountString, size);
655 if (isDiskOfType(TDEDiskDeviceType::DVDRAM)) {
656 ret =
DesktopIcon(
"media-optical-dvd" + mountString, size);
658 if (isDiskOfType(TDEDiskDeviceType::Zip)) {
659 ret =
DesktopIcon(
"media-floppy-zip" + mountString, size);
661 if (isDiskOfType(TDEDiskDeviceType::Tape)) {
662 ret =
DesktopIcon(
"media-tape" + mountString, size);
664 if (isDiskOfType(TDEDiskDeviceType::Camera)) {
665 ret =
DesktopIcon(
"camera" + TQString((mountPath() != TQString::null) ?
"_mount" :
"_umount"), size);
668 if (isDiskOfType(TDEDiskDeviceType::HDD)) {
669 ret =
DesktopIcon(
"drive-harddisk" + mountString, size);
670 if (checkDiskStatus(TDEDiskDeviceStatus::Hotpluggable)) {
671 ret =
DesktopIcon(
"media-flash-usb" + mountString, size);
673 if (isDiskOfType(TDEDiskDeviceType::CompactFlash)) {
674 ret =
DesktopIcon(
"media-flash-compact_flash" + mountString, size);
676 if (isDiskOfType(TDEDiskDeviceType::MemoryStick)) {
677 ret =
DesktopIcon(
"media-flash-memory_stick" + mountString, size);
679 if (isDiskOfType(TDEDiskDeviceType::SmartMedia)) {
680 ret =
DesktopIcon(
"media-flash-smart_media" + mountString, size);
682 if (isDiskOfType(TDEDiskDeviceType::SDMMC)) {
683 ret =
DesktopIcon(
"media-flash-sd_mmc" + mountString, size);
687 if (isDiskOfType(TDEDiskDeviceType::RAM)) {
690 if (isDiskOfType(TDEDiskDeviceType::Loop)) {
691 ret =
DesktopIcon(
"blockdevice" + mountString, size);
697 unsigned long long TDEStorageDevice::deviceSize() {
698 TQString bsnodename = systemPath();
701 TQString blocksize =
"512";
703 TQString dsnodename = systemPath();
704 dsnodename.append(
"/size");
705 TQFile dsfile( dsnodename );
707 if ( dsfile.open( IO_ReadOnly ) ) {
708 TQTextStream stream( &dsfile );
709 devicesize = stream.readLine();
713 return ((
unsigned long long)blocksize.toULong()*(
unsigned long long)devicesize.toULong());
716 TQString TDEStorageDevice::deviceFriendlySize() {
717 return TDEHardwareDevices::bytesToFriendlySizeString(deviceSize());
720 TQString TDEStorageDevice::mountPath() {
729 TDEGlobal::hardwareDevices()->rescanDeviceInformation(
this);
731 TQString dmnodename = systemPath();
732 dmnodename.append(
"/dm/name");
733 TQFile namefile( dmnodename );
735 if ( namefile.open( IO_ReadOnly ) ) {
736 TQTextStream stream( &namefile );
737 dmaltname = stream.readLine();
740 if (!dmaltname.isNull()) {
741 dmaltname.prepend(
"/dev/mapper/");
745 TQFile file(
"/proc/mounts" );
746 if ( file.open( IO_ReadOnly ) ) {
747 TQTextStream stream( &file );
749 while ( !stream.atEnd() ) {
750 line = stream.readLine();
751 TQStringList mountInfo = TQStringList::split(
" ", line,
true);
752 TQString testNode = *mountInfo.at(0);
754 if ((testNode == deviceNode()) || (testNode == dmaltname) || (testNode == (
"/dev/disk/by-uuid/" + diskUUID()))) {
755 TQString ret = *mountInfo.at(1);
756 ret.replace(
"\\040",
" ");
767 TQStringList slaveDeviceList = holdingDevices();
768 for ( TQStringList::Iterator slavedevit = slaveDeviceList.begin(); slavedevit != slaveDeviceList.end(); ++slavedevit ) {
770 TDEHardwareDevices *hwdevices = TDEGlobal::hardwareDevices();
771 TDEGenericDevice *hwdevice = hwdevices->findBySystemPath(*slavedevit);
772 if ((hwdevice) && (hwdevice->type() == TDEGenericDeviceType::Disk)) {
773 TDEStorageDevice* sdevice =
static_cast<TDEStorageDevice*
>(hwdevice);
774 return sdevice->mountPath();
778 return TQString::null;
781 TQString TDEStorageDevice::mountDevice(TQString mediaName, TDEStorageMountOptions mountOptions, TQString* errRet,
int* retcode) {
782 int internal_retcode;
784 retcode = &internal_retcode;
787 TQString ret = mountPath();
795 TQString devNode = deviceNode();
796 devNode.replace(
"'",
"'\\''");
797 mediaName.replace(
"'",
"'\\''");
799 #if defined(WITH_UDISKS2) || defined(WITH_UDISKS)
801 TQStringList udisksOptions;
802 TQString optionString;
804 if (mountOptions[
"ro"] ==
"true") {
805 udisksOptions.append(
"ro");
808 if (mountOptions[
"atime"] !=
"true") {
809 udisksOptions.append(
"noatime");
812 if (mountOptions[
"sync"] ==
"true") {
813 udisksOptions.append(
"sync");
816 if( (mountOptions[
"filesystem"] ==
"fat")
817 || (mountOptions[
"filesystem"] ==
"vfat")
818 || (mountOptions[
"filesystem"] ==
"msdos")
819 || (mountOptions[
"filesystem"] ==
"umsdos")
821 if (mountOptions.contains(
"shortname")) {
822 udisksOptions.append(TQString(
"shortname=%1").arg(mountOptions[
"shortname"]));
826 if( (mountOptions[
"filesystem"] ==
"jfs")) {
827 if (mountOptions[
"utf8"] ==
"true") {
833 if( (mountOptions[
"filesystem"] ==
"ntfs-3g") ) {
834 if (mountOptions.contains(
"locale")) {
835 udisksOptions.append(TQString(
"locale=%1").arg(mountOptions[
"locale"]));
839 if( (mountOptions[
"filesystem"] ==
"ext3")
840 || (mountOptions[
"filesystem"] ==
"ext4")
842 if (mountOptions.contains(
"journaling")) {
848 for (TQStringList::Iterator it = udisksOptions.begin(); it != udisksOptions.end(); ++it) {
849 optionString.append(
",");
850 optionString.append(*it);
853 if (!optionString.isEmpty()) {
854 optionString.remove(0, 1);
856 #endif // defined(WITH_UDISKS2) || defined(WITH_UDISKS)
859 if(command.isEmpty()) {
861 TQString errorString;
862 TQString fileSystemType;
864 if (mountOptions.contains(
"filesystem") && !mountOptions[
"filesystem"].isEmpty()) {
865 fileSystemType = mountOptions[
"filesystem"];
868 int uDisks2Ret = mountDriveUDisks2(devNode, fileSystemType, optionString, &errorString);
869 if (uDisks2Ret == 0) {
871 TDEGlobal::hardwareDevices()->processModifiedMounts();
876 else if (uDisks2Ret == -1) {
878 *errRet = errorString;
882 TDEGlobal::hardwareDevices()->processModifiedMounts();
889 command = TQString::null;
892 #endif // WITH_UDISKS2
895 if(command.isEmpty()) {
897 TQString errorString;
898 TQString fileSystemType;
900 if (mountOptions.contains(
"filesystem") && !mountOptions[
"filesystem"].isEmpty()) {
901 fileSystemType = mountOptions[
"filesystem"];
904 int uDisksRet = mountDriveUDisks(devNode, fileSystemType, udisksOptions, &errorString);
905 if (uDisksRet == 0) {
907 TDEGlobal::hardwareDevices()->processModifiedMounts();
912 else if (uDisksRet == -1) {
914 *errRet = errorString;
918 TDEGlobal::hardwareDevices()->processModifiedMounts();
925 command = TQString::null;
928 #endif // WITH_UDISKS
930 if(command.isEmpty()) {
933 if (!pmountProg.isEmpty()) {
935 KTempFile passwordFile(TQString::null,
"tmp", 0600);
936 passwordFile.setAutoDelete(
true);
938 TQString optionString;
939 if (mountOptions[
"ro"] ==
"true") {
940 optionString.append(
" -r");
943 if (mountOptions[
"atime"] !=
"true") {
944 optionString.append(
" -A");
947 if (mountOptions[
"utf8"] ==
"true") {
948 optionString.append(
" -c utf8");
951 if (mountOptions[
"sync"] ==
"true") {
952 optionString.append(
" -s");
955 if (mountOptions.contains(
"filesystem") && !mountOptions[
"filesystem"].isEmpty()) {
956 optionString.append(TQString(
" -t %1").arg(mountOptions[
"filesystem"]));
959 if (mountOptions.contains(
"locale")) {
960 optionString.append(TQString(
" -c %1").arg(mountOptions[
"locale"]));
964 if (mountOptions.contains(
"mountpoint")
965 && !mountOptions[
"mountpoint"].isEmpty()
966 && (mountOptions[
"mountpoint"] !=
"/media/")) {
967 mountpoint = mountOptions[
"mountpoint"];
968 mountpoint.replace(
"'",
"'\\''");
971 mountpoint = mediaName;
974 TQString passFileName = passwordFile.name();
975 passFileName.replace(
"'",
"'\\''");
977 command = TQString(
"pmount -p '%1' %2 '%3' '%4' 2>&1").arg(passFileName).arg(optionString).arg(devNode).arg(mountpoint);
981 if(command.isEmpty()) {
983 *errRet =
i18n(
"No supported mounting methods were detected on your system");
988 FILE *exepipe = popen(command.local8Bit(),
"r");
990 TQString mount_output;
991 TQTextStream* ts =
new TQTextStream(exepipe, IO_ReadOnly);
992 mount_output = ts->read();
994 *retcode = pclose(exepipe);
996 *errRet = mount_output;
1001 TDEGlobal::hardwareDevices()->processModifiedMounts();
1008 TQString TDEStorageDevice::mountEncryptedDevice(TQString passphrase, TQString mediaName, TDEStorageMountOptions mountOptions, TQString* errRet,
int* retcode) {
1009 int internal_retcode;
1011 retcode = &internal_retcode;
1014 TQString ret = mountPath();
1016 if (!ret.isNull()) {
1021 KTempFile passwordFile(TQString::null,
"tmp", 0600);
1022 passwordFile.setAutoDelete(
true);
1023 TQFile* pwFile = passwordFile.file();
1025 return TQString::null;
1028 pwFile->writeBlock(passphrase.ascii(), passphrase.length());
1031 TQString optionString;
1032 if (mountOptions[
"ro"] ==
"true") {
1033 optionString.append(
" -r");
1036 if (mountOptions[
"atime"] !=
"true") {
1037 optionString.append(
" -A");
1040 if (mountOptions[
"utf8"] ==
"true") {
1041 optionString.append(
" -c utf8");
1044 if (mountOptions[
"sync"] ==
"true") {
1045 optionString.append(
" -s");
1048 if (mountOptions.contains(
"filesystem") && !mountOptions[
"filesystem"].isEmpty()) {
1049 optionString.append(TQString(
" -t %1").arg(mountOptions[
"filesystem"]));
1052 if (mountOptions.contains(
"locale")) {
1053 optionString.append(TQString(
" -c %1").arg(mountOptions[
"locale"]));
1056 TQString passFileName = passwordFile.name();
1057 TQString devNode = deviceNode();
1058 passFileName.replace(
"'",
"'\\''");
1059 devNode.replace(
"'",
"'\\''");
1060 mediaName.replace(
"'",
"'\\''");
1061 TQString command = TQString(
"pmount -p '%1' %2 '%3' '%4' 2>&1").arg(passFileName).arg(optionString).arg(devNode).arg(mediaName);
1063 FILE *exepipe = popen(command.local8Bit(),
"r");
1065 TQString mount_output;
1066 TQTextStream* ts =
new TQTextStream(exepipe, IO_ReadOnly);
1067 mount_output = ts->read();
1069 *retcode = pclose(exepipe);
1071 *errRet = mount_output;
1076 TDEGlobal::hardwareDevices()->processModifiedMounts();
1083 bool TDEStorageDevice::unmountDevice(TQString* errRet,
int* retcode) {
1084 int internal_retcode;
1086 retcode = &internal_retcode;
1089 TQString mountpoint = mountPath();
1090 TQString devNode = deviceNode();
1092 if (mountpoint.isNull()) {
1096 mountpoint.replace(
"'",
"'\\''");
1101 if(command.isEmpty()) {
1103 TQString errorString;
1104 int unMountUDisks2Ret = unMountDriveUDisks2(devNode, TQString::null, &errorString);
1105 if (unMountUDisks2Ret == 0) {
1107 TDEGlobal::hardwareDevices()->processModifiedMounts();
1111 else if (unMountUDisks2Ret == -1) {
1113 *errRet = errorString;
1117 TDEGlobal::hardwareDevices()->processModifiedMounts();
1123 command = TQString::null;
1126 #endif // WITH_UDISKS2
1128 if(command.isEmpty()) {
1130 TQString errorString;
1131 int unMountUDisksRet = unMountDriveUDisks(devNode, TQStringList(), &errorString);
1132 if (unMountUDisksRet == 0) {
1134 TDEGlobal::hardwareDevices()->processModifiedMounts();
1138 else if (unMountUDisksRet == -1) {
1140 *errRet = errorString;
1144 TDEGlobal::hardwareDevices()->processModifiedMounts();
1150 command = TQString::null;
1153 #endif // WITH_UDISKS
1154 if(command.isEmpty() &&
1156 command = TQString(
"pumount '%1' 2>&1").arg(mountpoint);
1159 if(command.isEmpty()) {
1161 *errRet =
i18n(
"No supported unmounting methods were detected on your system");
1166 FILE *exepipe = popen(command.local8Bit(),
"r");
1168 TQString umount_output;
1169 TQTextStream* ts =
new TQTextStream(exepipe, IO_ReadOnly);
1170 umount_output = ts->read();
1172 *retcode = pclose(exepipe);
1173 if (*retcode == 0) {
1175 TDEGlobal::hardwareDevices()->processModifiedMounts();
1181 *errRet = umount_output;
1187 TDEGlobal::hardwareDevices()->processModifiedMounts();
1192 TQString TDEStorageDevice::determineFileSystemType(TQString path) {
1193 TQStringList mountTable;
1194 TQString prevPath = path;
1197 struct stat directory_info;
1198 if (path.startsWith(
"/")) {
1199 stat(path.local8Bit(), &directory_info);
1200 prevDev = directory_info.st_dev;
1203 while (path !=
"/") {
1204 pos = path.findRev(
"/", -1, TRUE);
1208 path = path.mid(0, pos);
1212 stat(path.local8Bit(), &directory_info);
1213 if (directory_info.st_dev != prevDev) {
1217 prevDev = directory_info.st_dev;
1223 TQFile file(
"/proc/mounts" );
1224 if ( file.open( IO_ReadOnly ) ) {
1225 TQTextStream stream( &file );
1226 while ( !stream.atEnd() ) {
1227 mountTable.append(stream.readLine());
1233 TQStringList::Iterator it;
1234 for ( it = mountTable.begin(); it != mountTable.end(); ++it ) {
1235 TQStringList mountInfo = TQStringList::split(
" ", (*it),
true);
1236 if ((*mountInfo.at(1)) == prevPath) {
1237 return (*mountInfo.at(2));
1242 return TQString::null;
1245 #include "tdestoragedevice.moc"
TQPixmap DesktopIcon(const TQString &name, int size=0, int state=TDEIcon::DefaultState, TDEInstance *instance=TDEGlobal::instance())
TQString i18n(const char *text)
static TDEStandardDirs * dirs()
Returns the application standard dirs object.
StdSizes
These are the standard sizes for icons.
static TQString findExe(const TQString &appname, const TQString &pathstr=TQString::null, bool ignoreExecBit=false)
Finds the executable in the system path.
TDEAction * open(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name=0)
The KTempFile class creates and opens a unique file for temporary use.
TDEAction * close(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name=0)
TQString label(StdAccel id)
Returns a localized label for user-visible display.