PCManFM-Qt
path.h
1 /*
2  * Copyright (C) 2014 Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  */
19 
20 #ifndef FM_PATH_H
21 #define FM_PATH_H
22 
23 #include "libfmqtglobals.h"
24 #include <libfm/fm.h>
25 #include <QString>
26 #include <QMetaType>
27 
28 namespace Fm {
29 
30 class LIBFM_QT_API Path {
31 public:
32 
33  Path(): data_(NULL) {
34  }
35 
36  Path(FmPath* path, bool takeOwnership = false): data_(path) {
37  if(path && !takeOwnership)
38  fm_path_ref(data_);
39  }
40 
41  Path(const Path& other): data_(other.data_ ? fm_path_ref(other.data_) : NULL) {
42  }
43 
44  Path(GFile* gf): data_(fm_path_new_for_gfile(gf)) {
45  }
46 
47  ~Path() {
48  if(data_)
49  fm_path_unref(data_);
50  }
51 
52  static Path fromPathName(const char* path_name) {
53  return Path(fm_path_new_for_path(path_name), true);
54  }
55 
56  static Path fromUri(const char* uri) {
57  return Path(fm_path_new_for_uri(uri), true);
58  }
59 
60  static Path fromDisplayName(const char* path_name) {
61  return Path(fm_path_new_for_display_name(path_name), true);
62  }
63 
64  static Path fromString(const char* path_str) {
65  return Path(fm_path_new_for_str(path_str), true);
66  }
67 
68  static Path fromCommandlineArg(const char* arg) {
69  return Path(fm_path_new_for_commandline_arg(arg), true);
70  }
71 
72  Path child(const char* basename) {
73  return Path(fm_path_new_child(data_, basename), true);
74  }
75 
76  Path child(const char* basename, int name_len) {
77  return Path(fm_path_new_child_len(data_, basename, name_len), true);
78  }
79 
80  Path relative(const char* rel) {
81  return Path(fm_path_new_relative(data_, rel), true);
82  }
83 
84  /* predefined paths */
85  static Path root(void) { /* / */
86  return Path(fm_path_get_root(), false);
87  }
88 
89  static Path home(void) { /* home directory */
90  return Path(fm_path_get_home(), false);
91  }
92 
93  static Path desktop(void) { /* $HOME/Desktop */
94  return Path(fm_path_get_desktop(), false);
95  }
96 
97  static Path trash(void) { /* trash:/// */
98  return Path(fm_path_get_trash(), false);
99  }
100 
101  static Path appsMenu(void) { /* menu://applications.menu/ */
102  return Path(fm_path_get_apps_menu(), false);
103  }
104 
105  Path parent() {
106  return Path(fm_path_get_parent(data_), false);
107  }
108 
109  const char* basename() {
110  return fm_path_get_basename(data_);
111  }
112 
113  FmPathFlags flags() {
114  return fm_path_get_flags(data_);
115  }
116 
117  bool hasPrefix(FmPath* prefix) {
118  return fm_path_has_prefix(data_, prefix);
119  }
120 
121  Path schemePath() {
122  return Path(fm_path_get_scheme_path(data_), true);
123  }
124 
125  bool isNative() {
126  return fm_path_is_native(data_);
127  }
128 
129  bool isTrash() {
130  return fm_path_is_trash(data_);
131  }
132 
133  bool isTrashRoot() {
134  return fm_path_is_trash_root(data_);
135  }
136 
137  bool isNativeOrTrash() {
138  return fm_path_is_native_or_trash(data_);
139  }
140 
141  char* toString() {
142  return fm_path_to_str(data_);
143  }
144 
145  QByteArray toByteArray() {
146  char* s = fm_path_to_str(data_);
147  QByteArray str(s);
148  g_free(s);
149  return str;
150  }
151 
152  char* toUri() {
153  return fm_path_to_uri(data_);
154  }
155 
156  GFile* toGfile() {
157  return fm_path_to_gfile(data_);
158  }
159 
160  /*
161  char* displayName(bool human_readable = true) {
162  return fm_path_display_name(data_, human_readable);
163  }
164  */
165 
166  QString displayName(bool human_readable = true) {
167  char* dispname = fm_path_display_name(data_, human_readable);
168  QString str = QString::fromUtf8(dispname);
169  g_free(dispname);
170  return str;
171  }
172 
173  /*
174  char* displayBasename() {
175  return fm_path_display_basename(data_);
176  }
177  */
178 
179  QString displayBasename() {
180  char* basename = fm_path_display_basename(data_);
181  QString s = QString::fromUtf8(basename);
182  g_free(basename);
183  return s;
184  }
185 
186  /* For used in hash tables */
187  guint hash() {
188  return fm_path_hash(data_);
189  }
190 
191  void take(FmPath* path) { // take the ownership of the "path"
192  if(data_)
193  fm_path_unref(data_);
194  data_ = path;
195  }
196 
197  Path& operator = (const Path& other) {
198  if(data_)
199  fm_path_unref(data_);
200  data_ = fm_path_ref(other.data_);
201  return *this;
202  }
203 
204  bool operator == (const Path& other) const {
205  return fm_path_equal(data_, other.data_);
206  }
207 
208  bool operator != (const Path& other) const {
209  return !fm_path_equal(data_, other.data_);
210  }
211 
212  bool operator < (const Path& other) const {
213  return compare(other);
214  }
215 
216  bool operator > (const Path& other) const {
217  return (other < *this);
218  }
219 
220  /* can be used for sorting */
221  int compare(const Path& other) const {
222  return fm_path_compare(data_, other.data_);
223  }
224 
225  /* used for completion in entry */
226  bool equal(const gchar *str, int n) const {
227  return fm_path_equal_str(data_, str, n);
228  }
229 
230  /* calculate how many elements are in this path. */
231  int depth() const {
232  return fm_path_depth(data_);
233  }
234 
235  FmPath* data() const {
236  return data_;
237  }
238 
239 private:
240  FmPath* data_;
241 };
242 
243 }
244 
245 Q_DECLARE_OPAQUE_POINTER(FmPath*)
246 
247 #endif // FM_PATH_H
Definition: appchoosercombobox.cpp:26
Definition: path.h:30