1 /*
2  * This file Copyright (C) 2009-2016 Mnemosyne LLC
3  *
4  * It may be used under the GNU GPL versions 2 or 3
5  * or any future license endorsed by Mnemosyne LLC.
6  *
7  */
8 
9 #pragma once
10 
11 #include <ctime>
12 
13 #include <QMainWindow>
14 #include <QNetworkReply>
15 #include <QPointer>
16 #include <QSystemTrayIcon>
17 #include <QTimer>
18 #include <QWidgetList>
19 
20 #include "Filters.h"
21 #include "Speed.h"
22 #include "TorrentFilter.h"
23 #include "Typedefs.h"
24 
25 #include "ui_MainWindow.h"
26 
27 class QAction;
28 class QIcon;
29 class QMenu;
30 class QStringList;
31 
32 class AboutDialog;
33 class AddData;
34 class DetailsDialog;
35 class Prefs;
36 class PrefsDialog;
37 class Session;
38 class SessionDialog;
39 class StatsDialog;
40 class TorrentDelegate;
41 class TorrentDelegateMin;
42 class TorrentModel;
43 
44 extern "C"
45 {
46 struct tr_variant;
47 }
48 
49 class MainWindow : public QMainWindow
50 {
51     Q_OBJECT
52 
53 public:
54     MainWindow(Session&, Prefs&, TorrentModel&, bool minized);
55     virtual ~MainWindow();
56 
trayIcon()57     QSystemTrayIcon& trayIcon()
58     {
59         return myTrayIcon;
60     }
61 
62 public slots:
63     void startAll();
64     void startSelected();
65     void startSelectedNow();
66     void pauseAll();
67     void pauseSelected();
68     void removeSelected();
69     void deleteSelected();
70     void verifySelected();
71     void queueMoveTop();
72     void queueMoveUp();
73     void queueMoveDown();
74     void queueMoveBottom();
75     void reannounceSelected();
76     void onNetworkTimer();
77 
78     void setToolbarVisible(bool);
79     void setFilterbarVisible(bool);
80     void setStatusbarVisible(bool);
81     void setCompactView(bool);
82     void wrongAuthentication();
83 
84     void openSession();
85 
86 protected:
87     // QWidget
88     void contextMenuEvent(QContextMenuEvent*) override;
89     void dragEnterEvent(QDragEnterEvent*) override;
90     void dropEvent(QDropEvent*) override;
91 
92 private:
93     QIcon getStockIcon(QString const&, int fallback = -1);
94     QIcon addEmblem(QIcon icon, QStringList const& emblemNames);
95 
96     torrent_ids_t getSelectedTorrents(bool withMetadataOnly = false) const;
97     void updateNetworkIcon();
98 
99     QMenu* createOptionsMenu();
100     QMenu* createStatsModeMenu();
101     void initStatusBar();
102 
103     void clearSelection();
104     void addTorrent(AddData const& addMe, bool showOptions);
105 
106     // QWidget
107     void hideEvent(QHideEvent* event) override;
108     void showEvent(QShowEvent* event) override;
109 
110 private slots:
111     void addTorrents(QStringList const& filenames);
112     void copyMagnetLinkToClipboard();
113     void dataReadProgress();
114     void dataSendProgress();
115     void newTorrent();
116     void onNetworkResponse(QNetworkReply::NetworkError code, QString const& message);
117     void onRefreshTimer();
118     void onSessionSourceChanged();
119     void onSetPrefs();
120     void onSetPrefs(bool);
121     void onSortModeChanged(QAction* action);
122     void onStatsModeChanged(QAction* action);
123     void openAbout();
124     void openDonate();
125     void openFolder();
126     void openHelp();
127     void openPreferences();
128     void openProperties();
129     void openStats();
130     void openTorrent();
131     void openURL();
132     void refreshPref(int key);
133     void refreshSoon(int fields = ~0);
134     void removeTorrents(bool const deleteFiles);
135     void setLocation();
136     void setSortAscendingPref(bool);
137     void toggleSpeedMode();
138     void toggleWindows(bool doShow);
139     void trayActivated(QSystemTrayIcon::ActivationReason);
140 
141 private:
142     Session& mySession;
143     Prefs& myPrefs;
144     TorrentModel& myModel;
145 
146     QPixmap myPixmapNetworkError;
147     QPixmap myPixmapNetworkIdle;
148     QPixmap myPixmapNetworkReceive;
149     QPixmap myPixmapNetworkTransmit;
150     QPixmap myPixmapNetworkTransmitReceive;
151 
152     Ui_MainWindow ui;
153 
154     time_t myLastFullUpdateTime;
155     QPointer<SessionDialog> mySessionDialog;
156     QPointer<PrefsDialog> myPrefsDialog;
157     QPointer<AboutDialog> myAboutDialog;
158     QPointer<StatsDialog> myStatsDialog;
159     QPointer<DetailsDialog> myDetailsDialog;
160     QSystemTrayIcon myTrayIcon;
161     TorrentFilter myFilterModel;
162     TorrentDelegate* myTorrentDelegate;
163     TorrentDelegateMin* myTorrentDelegateMin;
164     time_t myLastSendTime;
165     time_t myLastReadTime;
166     QTimer myNetworkTimer;
167     bool myNetworkError;
168     QAction* myDlimitOffAction;
169     QAction* myDlimitOnAction;
170     QAction* myUlimitOffAction;
171     QAction* myUlimitOnAction;
172     QAction* myRatioOffAction;
173     QAction* myRatioOnAction;
174     QWidgetList myHidden;
175     QWidget* myFilterBar;
176     QAction* myAltSpeedAction;
177     QString myErrorMessage;
178 
179     struct TransferStats
180     {
181         Speed speedUp;
182         Speed speedDown;
183         size_t peersSending = 0;
184         size_t peersReceiving = 0;
185     };
186     TransferStats getTransferStats() const;
187 
188     enum
189     {
190         REFRESH_TITLE = (1 << 0),
191         REFRESH_STATUS_BAR = (1 << 1),
192         REFRESH_TRAY_ICON = (1 << 2),
193         REFRESH_TORRENT_VIEW_HEADER = (1 << 3),
194         REFRESH_ACTION_SENSITIVITY = (1 << 4)
195     };
196     int myRefreshFields = 0;
197     QTimer myRefreshTimer;
198     void refreshActionSensitivity();
199     void refreshStatusBar(TransferStats const&);
200     void refreshTitle();
201     void refreshTorrentViewHeader();
202     void refreshTrayIcon(TransferStats const&);
203 };
204