PCManFM-Qt
fileoperation.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_FILEOPERATION_H
22 #define FM_FILEOPERATION_H
23 
24 #include "libfmqtglobals.h"
25 #include <QObject>
26 #include <QElapsedTimer>
27 #include <libfm/fm.h>
28 
29 class QTimer;
30 
31 namespace Fm {
32 
33 class FileOperationDialog;
34 
35 class LIBFM_QT_API FileOperation : public QObject {
36 Q_OBJECT
37 public:
38  enum Type {
39  Copy = FM_FILE_OP_COPY,
40  Move = FM_FILE_OP_MOVE,
41  Link = FM_FILE_OP_LINK,
42  Delete = FM_FILE_OP_DELETE,
43  Trash = FM_FILE_OP_TRASH,
44  UnTrash = FM_FILE_OP_UNTRASH,
45  ChangeAttr = FM_FILE_OP_CHANGE_ATTR
46  };
47 
48 public:
49  explicit FileOperation(Type type, FmPathList* srcFiles, QObject* parent = 0);
50  virtual ~FileOperation();
51 
52  void setDestination(FmPath* dest) {
53  destPath = fm_path_ref(dest);
54  fm_file_ops_job_set_dest(job_, dest);
55  }
56 
57  void setChmod(mode_t newMode, mode_t newModeMask) {
58  fm_file_ops_job_set_chmod(job_, newMode, newModeMask);
59  }
60 
61  void setChown(gint uid, gint gid) {
62  fm_file_ops_job_set_chown(job_, uid, gid);
63  }
64 
65  // This only work for change attr jobs.
66  void setRecursiveChattr(bool recursive) {
67  fm_file_ops_job_set_recursive(job_, (gboolean)recursive);
68  }
69 
70  bool run();
71 
72  void cancel() {
73  if(job_)
74  fm_job_cancel(FM_JOB(job_));
75  }
76 
77  bool isRunning() const {
78  return job_ ? fm_job_is_running(FM_JOB(job_)) : false;
79  }
80 
81  bool isCancelled() const {
82  return job_ ? fm_job_is_cancelled(FM_JOB(job_)) : false;
83  }
84 
85  FmFileOpsJob* job() {
86  return job_;
87  }
88 
89  bool autoDestroy() {
90  return autoDestroy_;
91  }
92  void setAutoDestroy(bool destroy = true) {
93  autoDestroy_ = destroy;
94  }
95 
96  Type type() {
97  return (Type)job_->type;
98  }
99 
100  // convinient static functions
101  static FileOperation* copyFiles(FmPathList* srcFiles, FmPath* dest, QWidget* parent = 0);
102  static FileOperation* moveFiles(FmPathList* srcFiles, FmPath* dest, QWidget* parent = 0);
103  static FileOperation* symlinkFiles(FmPathList* srcFiles, FmPath* dest, QWidget* parent = 0);
104  static FileOperation* deleteFiles(FmPathList* srcFiles, bool promp = true, QWidget* parent = 0);
105  static FileOperation* trashFiles(FmPathList* srcFiles, bool promp = true, QWidget* parent = 0);
106  static FileOperation* unTrashFiles(FmPathList* srcFiles, QWidget* parent = 0);
107  static FileOperation* changeAttrFiles(FmPathList* srcFiles, QWidget* parent = 0);
108 
109 Q_SIGNALS:
110  void finished();
111 
112 private:
113  static gint onFileOpsJobAsk(FmFileOpsJob* job, const char* question, char* const* options, FileOperation* pThis);
114  static gint onFileOpsJobAskRename(FmFileOpsJob* job, FmFileInfo* src, FmFileInfo* dest, char** new_name, FileOperation* pThis);
115  static FmJobErrorAction onFileOpsJobError(FmFileOpsJob* job, GError* err, FmJobErrorSeverity severity, FileOperation* pThis);
116  static void onFileOpsJobPrepared(FmFileOpsJob* job, FileOperation* pThis);
117  static void onFileOpsJobCurFile(FmFileOpsJob* job, const char* cur_file, FileOperation* pThis);
118  static void onFileOpsJobPercent(FmFileOpsJob* job, guint percent, FileOperation* pThis);
119  static void onFileOpsJobFinished(FmFileOpsJob* job, FileOperation* pThis);
120  static void onFileOpsJobCancelled(FmFileOpsJob* job, FileOperation* pThis);
121 
122  void handleFinish();
123  void disconnectJob();
124  void showDialog();
125 
126  void pauseElapsedTimer() {
127  if(Q_LIKELY(elapsedTimer_ != NULL)) {
128  lastElapsed_ += elapsedTimer_->elapsed();
129  elapsedTimer_->invalidate();
130  }
131  }
132 
133  void resumeElapsedTimer() {
134  if(Q_LIKELY(elapsedTimer_ != NULL)) {
135  elapsedTimer_->start();
136  }
137  }
138 
139  qint64 elapsedTime() {
140  if(Q_LIKELY(elapsedTimer_ != NULL)) {
141  return lastElapsed_ + elapsedTimer_->elapsed();
142  }
143  return 0;
144  }
145 
146 private Q_SLOTS:
147  void onUiTimeout();
148 
149 private:
150  FmFileOpsJob* job_;
151  FileOperationDialog* dlg;
152  FmPath* destPath;
153  FmPathList* srcPaths;
154  QTimer* uiTimer;
155  QElapsedTimer* elapsedTimer_;
156  qint64 lastElapsed_;
157  bool updateRemainingTime_;
158  QString curFile;
159  bool autoDestroy_;
160 };
161 
162 }
163 
164 #endif // FM_FILEOPERATION_H
Definition: appchoosercombobox.cpp:26
Definition: fileoperationdialog.h:36
Definition: fileoperation.h:35