1 /*
2  * Copyright (C) 2016 Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  */
19 
20 #ifndef __LIBFM_QT_FM2_FILE_INFO_H__
21 #define __LIBFM_QT_FM2_FILE_INFO_H__
22 
23 #include <QObject>
24 #include <QtGlobal>
25 #include "../libfmqtglobals.h"
26 
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 
32 #include <vector>
33 #include <set>
34 #include <utility>
35 #include <string>
36 #include <forward_list>
37 
38 #include "gioptrs.h"
39 #include "filepath.h"
40 #include "iconinfo.h"
41 #include "mimetype.h"
42 
43 
44 namespace Fm {
45 
46 class FileInfoList;
47 typedef std::set<unsigned int> HashSet;
48 
49 class LIBFM_QT_API FileInfo {
50 public:
51 
52     explicit FileInfo();
53 
54     explicit FileInfo(const GFileInfoPtr& inf, const FilePath& filePath, const FilePath& parentDirPath = FilePath());
55 
56     virtual ~FileInfo();
57 
canSetHidden()58     bool canSetHidden() const {
59         return isHiddenChangeable_;
60     }
61 
canSetIcon()62     bool canSetIcon() const {
63         return isIconChangeable_;
64     }
65 
canSetName()66     bool canSetName() const {
67         return isNameChangeable_;
68     }
69 
70     bool canThumbnail() const;
71 
gid()72     gid_t gid() const {
73         return gid_;
74     }
75 
uid()76     uid_t uid() const {
77         return uid_;
78     }
79 
filesystemId()80     const char* filesystemId() const {
81         return filesystemId_;
82     }
83 
fileId()84     const char* fileId() const {
85         return fileId_;
86     }
87 
icon()88     const std::shared_ptr<const IconInfo>& icon() const {
89         return icon_;
90     }
91 
mimeType()92     const std::shared_ptr<const MimeType>& mimeType() const {
93         return mimeType_;
94     }
95 
ctime()96     quint64 ctime() const {
97         return ctime_;
98     }
99 
crtime()100     quint64 crtime() const {
101         return crtime_;
102     }
103 
atime()104     quint64 atime() const {
105         return atime_;
106     }
107 
mtime()108     quint64 mtime() const {
109         return mtime_;
110     }
111 
dtime()112     quint64 dtime() const {
113         return dtime_;
114     }
target()115     const std::string& target() const {
116         return target_;
117     }
118 
isWritableDirectory()119     bool isWritableDirectory() const {
120         return (!isReadOnly_ && isDir());
121     }
122 
isAccessible()123     bool isAccessible() const {
124         return isAccessible_;
125     }
126 
isWritable()127     bool isWritable() const {
128         return isWritable_;
129     }
130 
isDeletable()131     bool isDeletable() const {
132         return isDeletable_;
133     }
134 
135     bool isExecutableType() const;
136 
isBackup()137     bool isBackup() const {
138         return isBackup_;
139     }
140 
isHidden()141     bool isHidden() const {
142         // FIXME: we might treat backup files as hidden
143         return isHidden_;
144     }
145 
isUnknownType()146     bool isUnknownType() const {
147         return mimeType_->isUnknownType();
148     }
149 
isDesktopEntry()150     bool isDesktopEntry() const {
151         return mimeType_->isDesktopEntry();
152     }
153 
isText()154     bool isText() const {
155         return mimeType_->isText();
156     }
157 
isImage()158     bool isImage() const {
159         return mimeType_->isImage();
160     }
161 
isMountable()162     bool isMountable() const {
163         return isMountable_;
164     }
165 
isShortcut()166     bool isShortcut() const {
167         return isShortcut_;
168     }
169 
isSymlink()170     bool isSymlink() const {
171         return S_ISLNK(mode_) ? true : false;
172     }
173 
isDir()174     bool isDir() const {
175         return S_ISDIR(mode_) || mimeType_->isDir();
176     }
177 
isNative()178     bool isNative() const {
179         return dirPath_ ? dirPath_.isNative() : path().isNative();
180     }
181 
canMount()182     bool canMount() const {
183         return canMount_;
184     }
185 
canUnmount()186     bool canUnmount() const {
187         return canUnmount_;
188     }
189 
canEject()190     bool canEject() const {
191         return canEject_;
192     }
193 
194 
mode()195     mode_t mode() const {
196         return mode_;
197     }
198 
realSize()199     uint64_t realSize() const {
200         return blksize_ *blocks_;
201     }
202 
size()203     uint64_t size() const {
204         return size_;
205     }
206 
name()207     const std::string& name() const {
208         return name_;
209     }
210 
displayName()211     const QString& displayName() const {
212         return dispName_;
213     }
214 
description()215     QString description() const {
216         return QString::fromUtf8(mimeType_ ? mimeType_->desc() : "");
217     }
218 
path()219     FilePath path() const {
220         return filePath_ ? filePath_ : dirPath_ ? dirPath_.child(name_.c_str()) : FilePath::fromPathStr(name_.c_str());
221     }
222 
dirPath()223     const FilePath& dirPath() const {
224         return dirPath_;
225     }
226 
227     void setFromGFileInfo(const GFileInfoPtr& inf, const FilePath& filePath, const FilePath& parentDirPath);
228 
emblems()229     const std::forward_list<std::shared_ptr<const IconInfo>>& emblems() const {
230         return emblems_;
231     }
232 
233     void setEmblem(const QString& emblmeName, bool setGFileEmblem = true) const;
234 
235     bool isTrustable() const;
236 
237     void setTrustable(bool trust) const;
238 
gFileInfo()239     GObjectPtr<GFileInfo> gFileInfo() const {
240         return inf_;
241     }
242 
243 private:
244     GObjectPtr<GFileInfo> inf_;
245     std::string name_;
246     QString dispName_;
247 
248     FilePath filePath_;
249     FilePath dirPath_;
250 
251     mode_t mode_;
252     const char* filesystemId_;
253     const char* fileId_;
254     uid_t uid_;
255     gid_t gid_;
256     uint64_t size_;
257     quint64 mtime_;
258     quint64 atime_;
259     quint64 ctime_;
260     quint64 crtime_;
261     quint64 dtime_;
262 
263     uint64_t blksize_;
264     uint64_t blocks_;
265 
266     std::shared_ptr<const MimeType> mimeType_;
267     std::shared_ptr<const IconInfo> icon_;
268     mutable std::forward_list<std::shared_ptr<const IconInfo>> emblems_;
269 
270     std::string target_; /* target of shortcut or mountable. */
271 
272     bool isShortcut_ : 1; /* TRUE if file is shortcut type */
273     bool isMountable_ : 1; /* TRUE if file is mountable type */
274     bool isAccessible_ : 1; /* TRUE if can be read by user */
275     bool isWritable_ : 1; /* TRUE if can be written to by user */
276     bool isDeletable_ : 1; /* TRUE if can be deleted by user */
277     bool isHidden_ : 1; /* TRUE if file is hidden */
278     bool isBackup_ : 1; /* TRUE if file is backup */
279     bool isNameChangeable_ : 1; /* TRUE if name can be changed */
280     bool isIconChangeable_ : 1; /* TRUE if icon can be changed */
281     bool isHiddenChangeable_ : 1; /* TRUE if hidden can be changed */
282     bool isReadOnly_ : 1; /* TRUE if host FS is R/O */
283     bool canMount_ : 1;  /* TRUE if can be mounted */
284     bool canUnmount_ : 1; /* TRUE if can be unmounted */
285     bool canEject_ : 1; /* TRUE if can be ejected */
286 };
287 
288 
289 class LIBFM_QT_API FileInfoList: public std::vector<std::shared_ptr<const FileInfo>> {
290 public:
291 
292     bool isSameType() const;
293 
294     bool isSameFilesystem() const;
295 
paths()296     FilePathList paths() const {
297         FilePathList ret;
298         for(auto& file: *this) {
299             ret.push_back(file->path());
300         }
301         return ret;
302     }
303 };
304 
305 // smart pointer to FileInfo objects (once created, FileInfo objects should stay immutable so const is needed here)
306 typedef std::shared_ptr<const FileInfo> FileInfoPtr;
307 
308 typedef std::pair<FileInfoPtr, FileInfoPtr> FileInfoPair;
309 
310 }
311 
312 Q_DECLARE_METATYPE(std::shared_ptr<const Fm::FileInfo>)
313 
314 
315 #endif // __LIBFM_QT_FM2_FILE_INFO_H__
316