port remaining windows backend from qtmobility.
[qt:qtsystems.git] / src / systeminfo / qstorageinfo_win.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtSystems module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qstorageinfo_win_p.h"
43
44 #if !defined( Q_CC_MINGW)
45 #ifndef Q_OS_WINCE
46 #include "windows/qwmihelper_win_p.h"
47 #endif
48 #endif
49
50 #include <QtCore/qdir.h>
51 #include <QtCore/qmetaobject.h>
52
53 #include <windows.h>
54
55 QT_BEGIN_NAMESPACE
56
57 QStorageInfoPrivate::QStorageInfoPrivate(QStorageInfo *parent)
58     : QObject(parent)
59     , q_ptr(parent)
60 {
61 #if !defined( Q_CC_MINGW)
62 #if !defined( Q_OS_WINCE)
63     WMIHelper *wHelper;
64     wHelper =  WMIHelper::instance();
65     wHelper->setWmiNamespace(QLatin1String("root/cimv2"));
66     wHelper->setClassName(QLatin1String("Win32_VolumeChangeEvent"));
67     QString aString = QLatin1String("SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE (TargetInstance ISA 'Win32_LogicalDisk') AND (TargetInstance.DriveType = 5 OR TargetInstance.DriveType = 2)");
68
69     wHelper->setupNotfication(QLatin1String("root/cimv2"),aString,QStringList());
70
71     connect(wHelper,SIGNAL(wminotificationArrived()),this,SLOT(notificationArrived()));
72 #endif
73 #endif
74 }
75
76 QStorageInfoPrivate::~QStorageInfoPrivate()
77 {
78 }
79
80 qlonglong QStorageInfoPrivate::availableDiskSpace(const QString &drive)
81 {
82     qlonglong availableBytes(-1);
83     if (!GetDiskFreeSpaceEx((WCHAR *)drive.utf16(), 0, 0, (PULARGE_INTEGER)&availableBytes))
84         availableBytes = -1;
85     return availableBytes;
86 }
87
88 qlonglong QStorageInfoPrivate::totalDiskSpace(const QString &drive)
89 {
90     qlonglong totalBytes(-1);
91     if (!GetDiskFreeSpaceEx((WCHAR *)drive.utf16(), 0, (PULARGE_INTEGER)&totalBytes, 0))
92         totalBytes = -1;
93     return totalBytes;
94 }
95
96 QString QStorageInfoPrivate::uriForDrive(const QString &drive)
97 {
98     WCHAR uri[50];
99     if (GetVolumeNameForVolumeMountPoint((WCHAR *)drive.utf16(), uri, 50))
100         return QString::fromUtf16(reinterpret_cast<const unsigned short *>(uri));
101     return QString();
102 }
103
104 QStringList QStorageInfoPrivate::allLogicalDrives()
105 {
106     mountEntriesList.clear();
107     QFileInfoList drives = QDir::drives();
108     foreach (const QFileInfo &drive, drives)
109         mountEntriesList << drive.absoluteFilePath();
110     return mountEntriesList;
111 }
112
113 QStorageInfo::DriveType QStorageInfoPrivate::driveType(const QString &drive)
114 {
115     UINT type = GetDriveType((WCHAR *)drive.utf16());
116     switch (type) {
117     case DRIVE_REMOVABLE:
118         return QStorageInfo::RemovableDrive;
119     case DRIVE_FIXED:
120         return QStorageInfo::InternalDrive;
121     case DRIVE_REMOTE:
122         return QStorageInfo::RemoteDrive;
123     case DRIVE_CDROM:
124         return QStorageInfo::CdromDrive;
125     case DRIVE_RAMDISK:
126         return QStorageInfo::RamDrive;
127     case DRIVE_UNKNOWN:
128     case DRIVE_NO_ROOT_DIR:
129     default:
130         return QStorageInfo::UnknownDrive;
131     };
132 }
133
134 void QStorageInfoPrivate::connectNotify(const QMetaMethod &signal)
135 {
136     static const QMetaMethod logicalDriveChangedSignal = QMetaMethod::fromSignal(&QStorageInfoPrivate::logicalDriveChanged);
137     if (signal == logicalDriveChangedSignal) {
138     }
139 }
140
141 void QStorageInfoPrivate::disconnectNotify(const QMetaMethod &signal)
142 {
143     static const QMetaMethod logicalDriveChangedSignal = QMetaMethod::fromSignal(&QStorageInfoPrivate::logicalDriveChanged);
144     if (signal == logicalDriveChangedSignal) {
145     }
146 }
147
148 void QStorageInfoPrivate::notificationArrived()
149 {
150     QStringList oldDrives;
151
152      oldDrives = mountEntriesList;
153      allLogicalDrives();
154
155      if (mountEntriesList.count() < oldDrives.count()) {
156          QStringListIterator i(oldDrives);
157           while (i.hasNext()) {
158              QString key = i.next();
159               if (!mountEntriesList.contains(key)) {
160                   emit logicalDriveChanged(key, false);
161               }
162           }
163       } else if (mountEntriesList.count() > oldDrives.count()) {
164          QStringListIterator i(mountEntriesList);
165           while (i.hasNext()) {
166              QString key = i.next();
167
168               if (oldDrives.contains(key))
169                   continue;
170                  emit logicalDriveChanged(key, true);
171          }
172      }
173  }
174
175 QT_END_NAMESPACE