1 /*
2  * Cantata
3  *
4  * Copyright (c) 2011-2020 Craig Drummond <craig.p.drummond@gmail.com>
5  *
6  * ----
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; see the file COPYING.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 
24 #ifndef REMOTEFSDEVICE_H
25 #define REMOTEFSDEVICE_H
26 
27 #include "fsdevice.h"
28 #include "config.h"
29 #include <QUrl>
30 
31 class QProcess;
32 class RemoteFsDevice : public FsDevice
33 {
34     Q_OBJECT
35 
36 public:
37     struct Details
38     {
DetailsDetails39         Details()
40             : configured(false) {
41         }
42         void load(const QString &group=QString());
43         void save() const;
44 
45         bool operator==(const Details &o) const {
46             return url==o.url && extraOptions==o.extraOptions;
47         }
48         bool operator!=(const Details &o) const {
49             return !(*this==o);
50         }
isEmptyDetails51         bool isEmpty() const {
52             return name.isEmpty() || url.isEmpty();
53         }
isLocalFileDetails54         bool isLocalFile() const {
55             return url.scheme().isEmpty() || constFileProtocol==url.scheme();
56         }
57 
58         QString name;
59         QUrl url;
60         QString serviceName;
61         QString extraOptions;
62         bool configured;
63     };
64 
65     static const QLatin1String constPromptPassword;
66     static const QLatin1String constSshfsProtocol;
67     static const QLatin1String constFileProtocol;
68 
69     static QList<Device *> loadAll(MusicLibraryModel *m);
70     static Device *create(MusicLibraryModel *m, const DeviceOptions &options, const Details &d);
71     static void renamed(const QString &oldName, const QString &newName);
72     static QString createUdi(const QString &n);
73 
74     RemoteFsDevice(MusicLibraryModel *m, const DeviceOptions &options, const Details &d);
75     RemoteFsDevice(MusicLibraryModel *m, const Details &d);
76     ~RemoteFsDevice() override;
77 
78     void toggle() override;
79     void mount();
80     void unmount();
supportsDisconnect()81     bool supportsDisconnect() const override { return !details.isLocalFile(); }
82     bool isConnected() const override;
83     double usedCapacity() override;
84     QString capacityString() override;
85     qint64 freeSpace() override;
86     void saveOptions() override;
87     void configure(QWidget *parent) override;
devType()88     DevType devType() const override { return RemoteFs; }
89     bool canPlaySongs() const override;
90     void destroy(bool removeFromConfig=true);
getDetails()91     const Details & getDetails() const { return details; }
subText()92     QString subText() override { return sub; }
93 
94 Q_SIGNALS:
95     void udiChanged();
96     void connectionStateHasChanged(const QString &id, bool connected);
97 
98 protected:
99     void load();
100     void setup();
101     void setAudioFolder() const override;
102 
103 private:
104     bool isOldSshfs();
105     QString settingsFileName() const;
106 
107 protected Q_SLOTS:
108     void saveProperties();
109     void saveProperties(const DeviceOptions &newOpts, const RemoteFsDevice::Details &newDetails);
110     void procFinished(int exitCode);
111     void mountStatus(const QString &mp, int pid, int st);
112     void umountStatus(const QString &mp, int pid, int st);
113 
114 protected:
115     mutable int mountToken;
116     mutable bool currentMountStatus;
117     Details details;
118     QProcess *proc;
119 //     QString audioFolderSetting;
120     bool messageSent;
121     QString sub;
122     friend class DevicesModel;
123 };
124 
125 #endif
126