1 /*
2  *    Copyright 2016 Kai Pastor
3  *
4  *    This file is part of OpenOrienteering.
5  *
6  *    OpenOrienteering is free software: you can redistribute it and/or modify
7  *    it under the terms of the GNU General Public License as published by
8  *    the Free Software Foundation, either version 3 of the License, or
9  *    (at your option) any later version.
10  *
11  *    OpenOrienteering is distributed in the hope that it will be useful,
12  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *    GNU General Public License for more details.
15  *
16  *    You should have received a copy of the GNU General Public License
17  *    along with OpenOrienteering.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 
21 #ifndef OPENORIENTEERING_STORAGE_LOCATION_H
22 #define OPENORIENTEERING_STORAGE_LOCATION_H
23 
24 
25 #include <memory>
26 #include <vector>
27 #include <type_traits>
28 
29 #include <QCoreApplication>
30 #include <QString>
31 
32 namespace OpenOrienteering {
33 
34 namespace Android {
35 
36 /**
37  * Tells the media scanner to register the given file or folder.
38  *
39  * This is required to make files quickly available for transfer via MTP.
40  */
41 void mediaScannerScanFile(const QString& path);
42 
43 }
44 
45 
46 // noexcept since Qt 5.5
47 constexpr bool qstring_is_nothrow_copy_constructible = std::is_nothrow_copy_constructible<QString>::value;
48 constexpr bool qstring_is_nothrow_move_constructible = std::is_nothrow_move_constructible<QString>::value;
49 constexpr bool qstring_is_nothrow_copy_assignable = std::is_nothrow_copy_assignable<QString>::value;
50 constexpr bool qstring_is_nothrow_move_assignable = std::is_nothrow_move_assignable<QString>::value;
51 
52 
53 /**
54  * Provides information about document storage locations.
55  */
56 class StorageLocation
57 {
58 	Q_DECLARE_TR_FUNCTIONS(OpenOrienteering::StorageLocation)
59 
60 public:
61 	/** Various hints about locations. */
62 	enum Hint {
63 		HintNormal,       ///< Normal location
64 		HintApplication,  ///< Location which might get cleaned unexpectedly
65 		HintReadOnly,     ///< Read-only location
66 		HintNoAccess,     ///< Location which requires to establish AppPermissions::StorageAccess
67 		HintInvalid       ///< Not a valid location at all
68 	};
69 
70 	/** Constructs a new location. */
71 	StorageLocation(const QString& path, Hint hint) noexcept;
72 
73 	/** Default copy constructor. */
74 	StorageLocation(const StorageLocation&) noexcept(qstring_is_nothrow_copy_constructible) = default;
75 
76 	/** Default move constructor. */
77 	StorageLocation(StorageLocation&&) noexcept(qstring_is_nothrow_move_constructible) = default;
78 
79 	StorageLocation& operator=(const StorageLocation&) noexcept(qstring_is_nothrow_copy_assignable) = default;
80 
81 	StorageLocation& operator=(StorageLocation&&) noexcept(qstring_is_nothrow_copy_assignable) = default;
82 
83 
84 	/** Returns the path of this location. */
85 	QString path() const;
86 
87 	/**
88 	 * Returns the hint for this location.
89 	 *
90 	 * This function returns an empty string for HintNormal.
91 	 */
92 	Hint hint() const;
93 
94 	/** Returns the text representing the hint for this location. */
95 	QString hintText() const;
96 
97 	/**
98 	 * Returns a text template for giving the hint for the given path.
99 	 *
100 	 * This function returns an empty string for HintNormal.
101 	 */
102 	static QString fileHintTextTemplate(Hint hint);
103 
104 
105 	/**
106 	 * Returns the known locations for documents.
107 	 *
108 	 * The returned shared-ptr will always have an object, even if it is an empty list.
109 	 */
110 	static std::shared_ptr<const std::vector<StorageLocation>> knownLocations();
111 
112 
113 	/** Forces a new scan of locations on the next call to knownLocations(). */
114 	static void refresh();
115 
116 
117 private:
118 	QString m_path;
119 	Hint    m_hint;
120 };
121 
122 
123 
124 inline
StorageLocation(const QString & path,StorageLocation::Hint hint)125 StorageLocation::StorageLocation(const QString& path, StorageLocation::Hint hint) noexcept
126 : m_path { path }
127 , m_hint { hint }
128 {
129 	// nothing else
130 }
131 
132 inline
path()133 QString StorageLocation::path() const
134 {
135 	return m_path;
136 }
137 
138 inline
hint()139 StorageLocation::Hint StorageLocation::hint() const
140 {
141 	return m_hint;
142 }
143 
144 
145 }  // namespace OpenOrienteering
146 
147 #endif // OPENORIENTEERING_STORAGE_LOCATION_H
148