PCManFM-Qt
mountoperation.h
1 /*
2 
3  Copyright (C) 2013 Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 
21 #ifndef FM_MOUNTOPERATION_H
22 #define FM_MOUNTOPERATION_H
23 
24 #include "libfmqtglobals.h"
25 #include <QWidget>
26 #include <QDialog>
27 #include <libfm/fm.h>
28 #include <gio/gio.h>
29 #include <QPointer>
30 
31 class QEventLoop;
32 
33 namespace Fm {
34 
35 // FIXME: the original APIs in gtk+ version of libfm for mounting devices is poor.
36 // Need to find a better API design which make things fully async and cancellable.
37 
38 // FIXME: parent_ does not work. All dialogs shown by the mount operation has no parent window assigned.
39 // FIXME: Need to reconsider the propery way of API design. Blocking sync calls are handy, but
40 // indeed causes some problems. :-(
41 
42 class LIBFM_QT_API MountOperation: public QObject {
43 Q_OBJECT
44 
45 public:
46  explicit MountOperation(bool interactive = true, QWidget* parent = 0);
47  ~MountOperation();
48 
49  void mount(FmPath* path) {
50  GFile* gf = fm_path_to_gfile(path);
51  g_file_mount_enclosing_volume(gf, G_MOUNT_MOUNT_NONE, op, cancellable_, (GAsyncReadyCallback)onMountFileFinished, new QPointer<MountOperation>(this));
52  g_object_unref(gf);
53  }
54 
55  void mount(GVolume* volume) {
56  g_volume_mount(volume, G_MOUNT_MOUNT_NONE, op, cancellable_, (GAsyncReadyCallback)onMountVolumeFinished, new QPointer<MountOperation>(this));
57  }
58 
59  void unmount(GMount* mount) {
60  prepareUnmount(mount);
61  g_mount_unmount_with_operation(mount, G_MOUNT_UNMOUNT_NONE, op, cancellable_, (GAsyncReadyCallback)onUnmountMountFinished, new QPointer<MountOperation>(this));
62  }
63 
64  void unmount(GVolume* volume) {
65  GMount* mount = g_volume_get_mount(volume);
66  if(!mount)
67  return;
68  unmount(mount);
69  g_object_unref(mount);
70  }
71 
72  void eject(GMount* mount) {
73  prepareUnmount(mount);
74  g_mount_eject_with_operation(mount, G_MOUNT_UNMOUNT_NONE, op, cancellable_, (GAsyncReadyCallback)onEjectMountFinished, new QPointer<MountOperation>(this));
75  }
76 
77  void eject(GVolume* volume) {
78  GMount* mnt = g_volume_get_mount(volume);
79  prepareUnmount(mnt);
80  g_object_unref(mnt);
81  g_volume_eject_with_operation(volume, G_MOUNT_UNMOUNT_NONE, op, cancellable_, (GAsyncReadyCallback)onEjectVolumeFinished, new QPointer<MountOperation>(this));
82  }
83 
84  QWidget* parent() const {
85  return parent_;
86  }
87 
88  void setParent(QWidget* parent) {
89  parent_ = parent;
90  }
91 
92  GCancellable* cancellable() const {
93  return cancellable_;
94  }
95 
96  GMountOperation* mountOperation() {
97  return op;
98  }
99 
100  void cancel() {
101  g_cancellable_cancel(cancellable_);
102  }
103 
104  bool isRunning() const {
105  return running;
106  }
107 
108  // block the operation used an internal QEventLoop and returns
109  // only after the whole operation is finished.
110  bool wait();
111 
112  bool autoDestroy() {
113  return autoDestroy_;
114  }
115 
116  void setAutoDestroy(bool destroy = true) {
117  autoDestroy_ = destroy;
118  }
119 
120 Q_SIGNALS:
121  void finished(GError* error = NULL);
122 
123 private:
124  void prepareUnmount(GMount* mount);
125 
126  static void onAskPassword(GMountOperation *_op, gchar* message, gchar* default_user, gchar* default_domain, GAskPasswordFlags flags, MountOperation* pThis);
127  static void onAskQuestion(GMountOperation *_op, gchar* message, GStrv choices, MountOperation* pThis);
128  // static void onReply(GMountOperation *_op, GMountOperationResult result, MountOperation* pThis);
129 
130  static void onAbort(GMountOperation *_op, MountOperation* pThis);
131  static void onShowProcesses(GMountOperation *_op, gchar* message, GArray* processes, GStrv choices, MountOperation* pThis);
132  static void onShowUnmountProgress(GMountOperation *_op, gchar* message, gint64 time_left, gint64 bytes_left, MountOperation* pThis);
133 
134  // it's possible that this object is freed when the callback is called by gio, so guarding with QPointer is needed here.
135  static void onMountFileFinished(GFile* file, GAsyncResult *res, QPointer<MountOperation>* pThis);
136  static void onMountVolumeFinished(GVolume* volume, GAsyncResult *res, QPointer<MountOperation>* pThis);
137  static void onUnmountMountFinished(GMount* mount, GAsyncResult *res, QPointer<MountOperation>* pThis);
138  static void onEjectMountFinished(GMount* mount, GAsyncResult *res, QPointer<MountOperation>* pThis);
139  static void onEjectVolumeFinished(GVolume* volume, GAsyncResult *res, QPointer<MountOperation>* pThis);
140 
141  void handleFinish(GError* error);
142 
143 private:
144  GMountOperation* op;
145  GCancellable* cancellable_;
146  QWidget* parent_;
147  bool running;
148  bool interactive_;
149  QEventLoop* eventLoop;
150  bool autoDestroy_;
151 };
152 
153 }
154 
155 #endif // FM_MOUNTOPERATION_H
Definition: appchoosercombobox.cpp:26
Definition: mountoperation.h:42