1 /*
2  * DesktopGwtCallback.hpp
3  *
4  * Copyright (C) 2021 by RStudio, PBC
5  *
6  * Unless you have received this program directly from RStudio pursuant
7  * to the terms of a commercial license agreement with RStudio, then
8  * this program is licensed to you under the terms of version 3 of the
9  * GNU Affero General Public License. This program is distributed WITHOUT
10  * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
11  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
12  * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
13  *
14  */
15 
16 #ifndef DESKTOP_GWT_CALLBACK_HPP
17 #define DESKTOP_GWT_CALLBACK_HPP
18 
19 #include <QObject>
20 #include <QClipboard>
21 #include <QKeySequence>
22 #include <QJsonArray>
23 #include <QJsonObject>
24 #include <QPrinter>
25 
26 #include <boost/optional.hpp>
27 
28 #ifdef Q_OS_WIN32
29 #include "DesktopWordViewer.hpp"
30 #include "DesktopPowerpointViewer.hpp"
31 #endif
32 
33 namespace rstudio {
34 namespace desktop {
35 
36 class MainWindow;
37 class GwtWindow;
38 class Synctex;
39 class JobLauncher;
40 
41 enum PendingQuit
42 {
43    PendingQuitNone             = 0,
44    PendingQuitAndExit          = 1,
45    PendingQuitAndRestart       = 2,
46    PendingQuitRestartAndReload = 3
47 };
48 
49 enum InputType
50 {
51    InputRequiredText = 0,
52    InputOptionalText = 1,
53    InputPassword     = 2,
54    InputNumeric      = 3
55 };
56 
57 class GwtCallback : public QObject
58 {
59    Q_OBJECT
60 
61 public:
62    GwtCallback(MainWindow* pMainWindow, GwtWindow* pOwner, bool isRemoteDesktop);
63    void initialize();
64    int collectPendingQuitRequest();
65 
66 Q_SIGNALS:
67    void workbenchInitialized();
68    void sessionQuit();
69 
70 public Q_SLOTS:
71    QString proportionalFont();
72    QString fixedWidthFont();
73    void browseUrl(QString url);
74 
75    QString getOpenFileName(const QString& caption,
76                            const QString& label,
77                            const QString& dir,
78                            const QString& filter,
79                            bool canChooseDirectories,
80                            bool focusOwner);
81 
82    QString getSaveFileName(const QString& caption,
83                            const QString& label,
84                            const QString& dir,
85                            const QString& defaultExtension,
86                            bool forceDefaultExtension,
87                            bool focusOwner);
88 
89    QString getExistingDirectory(const QString& caption,
90                                 const QString& label,
91                                 const QString& dir,
92                                 bool focusOwner);
93 
94    void onClipboardSelectionChanged();
95 
96    void undo();
97    void redo();
98 
99    void clipboardCut();
100    void clipboardCopy();
101    void clipboardPaste();
102 
103    void setClipboardText(QString text);
104    QString getClipboardText();
105    QJsonArray getClipboardUris();
106    QString getClipboardImage();
107 
108    void setGlobalMouseSelection(QString selection);
109    QString getGlobalMouseSelection();
110 
111    QJsonObject getCursorPosition();
112    bool doesWindowExistAtCursorPosition();
113 
114    void onWorkbenchInitialized(QString scratchPath);
115    void showFolder(QString path);
116    void showFile(QString path);
117    void showWordDoc(QString path);
118    void showPptPresentation(QString path);
119    void showPDF(QString path, int pdfPage);
120    void prepareShowWordDoc();
121    void prepareShowPptPresentation();
122 
123    // R version selection currently Win32 only
124    QString getRVersion();
125    QString chooseRVersion();
126 
127    double devicePixelRatio();
128 
129    void openMinimalWindow(QString name, QString url, int width, int height);
130    void activateMinimalWindow(QString name);
131    void activateSatelliteWindow(QString name);
132    void prepareForSatelliteWindow(QString name, int x, int y, int width,
133                                   int height);
134    void prepareForNamedWindow(QString name, bool allowExternalNavigate,
135                               bool showToolbar);
136    void closeNamedWindow(QString name);
137 
138    // coordinates are relative to entire containing web page
139    void copyPageRegionToClipboard(int left, int top, int width, int height);
140    void exportPageRegionToFile(QString targetPath,
141                                QString format,
142                                int left,
143                                int top,
144                                int width,
145                                int height);
146 
147    void printText(QString text);
148    void paintPrintText(QPrinter* printer);
149    void printFinished(int result);
150 
151    bool supportsClipboardMetafile();
152 
153    int showMessageBox(int type,
154                       QString caption,
155                       QString message,
156                       QString buttons,
157                       int defaultButton,
158                       int cancelButton);
159 
160    QString promptForText(QString title,
161                          QString caption,
162                          QString defaultValue,
163                          int type,
164                          QString rememberPasswordPrompt,
165                          bool rememberByDefault,
166                          int selectionStart,
167                          int selectionLength,
168                          QString okButtonCaption);
169 
170    void bringMainFrameToFront();
171    void bringMainFrameBehindActive();
172 
173    QString desktopRenderingEngine();
174    void setDesktopRenderingEngine(QString engine);
175 
176    QString filterText(QString text);
177 
178    void cleanClipboard(bool stripHtml);
179 
180    void setPendingQuit(int pendingQuit);
181 
182    void openProjectInNewWindow(QString projectFilePath);
183    void openSessionInNewWindow(QString workingDirectoryPath);
184 
185    void openTerminal(QString terminalPath,
186                      QString workingDirectory,
187                      QString extraPathEntries,
188                      QString shellType);
189 
190    QString getFixedWidthFontList();
191    QString getFixedWidthFont();
192    void setFixedWidthFont(QString font);
193 
194    QString getZoomLevels();
195    double getZoomLevel();
196    void setZoomLevel(double zoomLevel);
197 
198    void zoomIn();
199    void zoomOut();
200    void zoomActualSize();
201 
202    void setBackgroundColor(QJsonArray rgbColor);
203    void changeTitleBarColor(int red, int green, int blue);
204    void syncToEditorTheme(bool isDark);
205 
206    bool getEnableAccessibility();
207    void setEnableAccessibility(bool enable);
208 
209    bool getClipboardMonitoring();
210    void setClipboardMonitoring(bool monitoring);
211 
212    bool getIgnoreGpuExclusionList();
213    void setIgnoreGpuExclusionList(bool ignore);
214 
215    bool getDisableGpuDriverBugWorkarounds();
216    void setDisableGpuDriverBugWorkarounds(bool disable);
217 
218    void showLicenseDialog();
219    void showSessionServerOptionsDialog();
220    QString getInitMessages();
221    QString getLicenseStatusMessage();
222    bool allowProductUsage();
223 
224    QString getDesktopSynctexViewer();
225 
226    void externalSynctexPreview(QString pdfPath, int page);
227 
228    void externalSynctexView(const QString& pdfFile,
229                             const QString& srcFile,
230                             int line,
231                             int column);
232 
233    bool supportsFullscreenMode();
234    void toggleFullscreenMode();
235    void showKeyboardShortcutHelp();
236 
237    void launchSession(bool reload);
238 
239    void reloadZoomWindow();
240 
241    void setTutorialUrl(QString url);
242 
243    void setViewerUrl(QString url);
244    void reloadViewerZoomWindow(QString url);
245 
246    void setShinyDialogUrl(QString url);
247 
248    QString getScrollingCompensationType();
249 
250    bool isMacOS();
251    bool isCentOS();
252 
253    void setBusy(bool busy);
254 
255    void setWindowTitle(QString title);
256 
257    void installRtools(QString version, QString installerPath);
258 
259    QString getDisplayDpi();
260 
261    void onSessionQuit();
262 
263    QJsonObject getSessionServer();
264    QJsonArray getSessionServers();
265    void reconnectToSessionServer(const QJsonValue& sessionServerJson);
266 
267    bool setLauncherServer(const QJsonObject& sessionServerJson);
268    void connectToLauncherServer();
269 
270    QJsonObject getLauncherServer();
271    void startLauncherJobStatusStream(QString jobId);
272    void stopLauncherJobStatusStream(QString jobId);
273    void startLauncherJobOutputStream(QString jobId);
274    void stopLauncherJobOutputStream(QString jobId);
275    void controlLauncherJob(QString jobId, QString operation);
276    void submitLauncherJob(const QJsonObject& job);
277    void getJobContainerUser();
278    void validateJobsConfig();
279    int getProxyPortNumber();
280 
281    void signOut();
282 
283 private:
284    Synctex& synctex();
285    void activateAndFocusOwner();
286 
287 private:
288    void doAction(const QKeySequence& keys);
289    void doAction(QKeySequence::StandardKey key);
290    MainWindow* pMainWindow_;
291    GwtWindow* pOwner_;
292    JobLauncher* pLauncher_;
293    bool isRemoteDesktop_;
294    Synctex* pSynctex_;
295    int pendingQuit_;
296    QString printText_;
297 #ifdef Q_OS_WIN32
298    // viewers for Office file formats
299    WordViewer wordViewer_;
300    PowerpointViewer pptViewer_;
301 #endif
302 
303 };
304 
305 } // namespace desktop
306 } // namespace rstudio
307 
308 #endif // DESKTOP_GWT_CALLBACK_HPP
309