Filer
CustomFileSystemModel.h
1 /*-
2  * Copyright (c) 2022-23 Simon Peter <probono@puredarwin.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #ifndef CUSTOMFILESYSTEMMODEL_H
28 #define CUSTOMFILESYSTEMMODEL_H
29 
30 #include <QFileSystemModel>
31 #include <QByteArray>
32 #include "LaunchDB.h"
33 #include "CustomFileIconProvider.h"
34 
35 // NOTE: Qt::UserRole + 1 is already used by QFileSystemModel for the file path
36 static const int OpenWithRole = Qt::UserRole + 10;
37 static const int CanOpenRole = Qt::UserRole + 11;
38 static const int IsApplicationRole = Qt::UserRole + 12;
39 
40 class CustomFileSystemModel : public QFileSystemModel
41 {
42 Q_OBJECT
43 public:
44  explicit CustomFileSystemModel(QObject* parent = nullptr);
45 
46  ~CustomFileSystemModel() override;
47 
48  QVariant data(const QModelIndex& index, int role = Qt::ToolTipRole) const override;
49 
50  QByteArray readExtendedAttribute(const QModelIndex& index, const QString& attributeName) const;
51 
52  // Public method to access data from CustomFileSystemModel because we cannot access data from QFileSystemModel directly
53  QVariant fileData(const QModelIndex& index, int role) const;
54 
55  // Public method to access the "open-with" attribute
56  // NOTE: Would like to do this with an index, but don't have a valid index when this gets called for unknown reasons
57  // So we'll use the QFileInfo instead for now
58  QString openWith(const QFileInfo& fileInfo) const;
59 
60  // This gets called when a file is dropped onto the view
61  bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override;
62 
63  // These functions are used to set the supported drag and drop actions for the model.
64  Qt::DropActions supportedDropActions() const override;
65  Qt::DropActions supportedDragActions() const override;
66  Qt::ItemFlags flags(const QModelIndex &index) const override;
67  bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const override;
68 
69  void setPositionForIndex(const QPoint& position, const QModelIndex& index) const;
70  QPoint& getPositionForIndex(const QModelIndex& index) const;
71 
72  void persistItemPositions() const;
73 
74  bool setData(const QModelIndex &idx, const QVariant &value, int role) override;
75 
76  void removeCustomCoordinates(const QModelIndex& index) const;
77 
78 private:
79  // Private member variable to store "open-with" attributes.
80  mutable QMap<QModelIndex, QByteArray> openWithAttributes;
81 
82  // Private member variable to store "open-with" attributes.
83  mutable QMap<QModelIndex, QByteArray> canOpenAttributes;
84 
85  // Private member variable to store icon coordinates.
86  mutable QMap<QModelIndex, QPoint> iconCoordinates;
87 
88  // Private member variable to store whether an item is an application.
89  mutable QMap<QModelIndex, bool> isApplication;
90 
91  LaunchDB ldb;
92 
93  // Private method to create a bookmark file via drag and drop, e.g., from a web browser
94  bool createBrowserBookmarkFile(const QMimeData *data, QString dropTargetPath) const;
95 
96  // Private helper method to make a filename safe for the filesystem, e.g., for saving bookmarks from a web browser
97  QString makeFilenameSafe(const QString& input) const;
98 
99  // The following class is defined in "CustomFileIconProvider.h"; why does the compiler not see it?
100  // This is because the compiler does not see the definition of the class CustomFileIconProvider
101  // in the header file CustomFileIconProvider.h for some reason. Why?
102  // It is defined in the same directory as this file, and the header file is included.
103  CustomFileIconProvider *m_IconProvider;
104  // FIXME: Why are we getting error: unknown type name 'CustomFileIconProvider'; did you mean 'QFileIconProvider'?
105 
106 };
107 
108 #endif // CUSTOMFILESYSTEMMODEL_H
Definition: CustomFileIconProvider.h:9
Definition: CustomFileSystemModel.h:41
The LaunchDB class provides functionality to retrieve the default application associated with a file.
Definition: LaunchDB.h:43