android: Fix XML form filter.
[qpwmc.git] / pwmdMainWindow.h
blobf3155b0cd69605b772e089a0309d8637d0b0c7b8
1 /*
2 Copyright (C) 2010-2023 Ben Kibbey <bjk@luxsci.net>
4 This file is part of qpwmc.
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 This library 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 GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 USA
21 #ifndef PWMDMAINWINDOW_H
22 #define PWMDMAINWINDOW_H
24 #include <QMainWindow>
25 #include <QTreeWidget>
26 #include <QListWidget>
27 #include <QValidator>
28 #include <QStringList>
29 #include <QClipboard>
30 #include <QToolBar>
31 #include <QProgressBar>
32 #include <QPushButton>
33 #include <QUrl>
35 #include "pwmd.h"
36 #include "pwmdRemoteHost.h"
37 #include "pwmdClientDialog.h"
38 #include "pwmdOptionsDialog.h"
39 #include "pwmdFileOptionsDialog.h"
40 #include "pwmdOpenDialog.h"
41 #include "pwmdSearchDialog.h"
42 #ifdef Q_OS_ANDROID
43 #include "pwmdAndroidJNI.h"
44 #endif
45 #include "ui_pwmdMainWindow.h"
47 class ClientInfo;
48 class PwmdFileOptionsWidget;
50 class PwmdMainWindow : public QMainWindow
52 Q_OBJECT
53 public:
54 // The 'socket' parameter may be a PwmdRemoteHost that has been
55 // previously created in the Options and is the label of the
56 // configured host. If not found then it is treated as a local socket.
57 PwmdMainWindow(const QString & filename = 0,
58 const QString & name = "qpwmc", bool lock = true,
59 const QString & path = 0, const QString & socket = 0,
60 QWidget * parent = 0);
61 ~PwmdMainWindow();
63 // Returns the element path of the selected item in the element tree
64 // with each element separated by the specified character.
65 QString elementPath (const QString & sep = "\t");
67 // Returns the opened data filename.
68 QString filename ();
70 // Returns the URL of the connected socket.
71 QString socket ();
73 private slots:
74 void slotClientLockStateChanged (bool);
75 void slotElementSelected (QTreeWidgetItem *, QTreeWidgetItem *);
76 void slotElementClicked (QTreeWidgetItem *, int);
77 void slotToggleHiddenContent (bool);
78 void slotRefreshElementTree ();
79 void slotContentChanged ();
80 void slotContentModified (bool);
81 void slotNewElement ();
82 void slotNewRootElement ();
83 void slotNewElementFromPopup ();
84 void slotNewSiblingElement ();
85 void slotNewSiblingElementFromPopup ();
86 void slotDeleteElement ();
87 void slotRenameElement ();
88 void slotSearchShiftEnterPressed ();
89 void slotAdvancedSearchAction ();
90 void slotAdvancedSearch (const QString &);
91 void slotAdvancedSearchNext (const QString &, bool, bool, bool, bool, bool);
92 void slotAdvancedSearchFinished (int);
93 void slotFindElement ();
94 void slotFindTextChanged (const QString &);
95 void slotFindFinished ();
96 void slotEditElements ();
97 void slotResolveTargets ();
98 void slotExpandAll (bool);
99 void slotReload ();
100 void slotDndCopyElement ();
101 void slotDndMoveElement ();
102 void slotDndCreateTarget ();
103 void slotExpandElement ();
104 void slotCollapseElement ();
105 void slotRenameElementUpdate (QWidget *);
106 void slotElementEntered (QTreeWidgetItem *, int);
107 void slotElementEditorClosed (QWidget *,
108 QAbstractItemDelegate::EndEditHint);
109 void slotConnectionStateChanged (Pwmd::ConnectionState);
110 void slotInsertContent ();
111 void slotRefreshContent ();
112 void slotSaveToFile ();
113 void slotCopyContentToClipboard ();
114 void slotSaveContent ();
115 void slotToggleBase64Content (bool);
116 void slotMenuItemHovered (QAction *);
117 void slotStatusMessage (QString, void *);
118 void slotBusy (int, bool);
119 void slotSocketOptions ();
120 void slotGeneralOptions ();
121 void slotClientList ();
122 void slotConnectSocket ();
123 void slotDisconnectSocket ();
124 void slotOpenFile ();
125 void slotOpenFileDone (int);
126 void slotOpenForm ();
127 void slotCloseFile ();
128 void slotDoOpenFile ();
129 void slotEditAttributes ();
130 void slotEditTargetAttributes (bool);
131 void slotRefreshAttributes ();
132 void slotAttrSearchAndReplace ();
133 void slotAttrSearchAndReplaceChanged (const QString &);
134 void slotAttributeSelected (QListWidgetItem * item, QListWidgetItem * old);
135 void slotAttributeEditorClosed (QWidget *,
136 QAbstractItemDelegate::EndEditHint);
137 void slotConfirmNewAttribute (QWidget *);
138 void slotNewAttribute ();
139 void slotDeleteAttribute ();
140 void setElementColors (QTreeWidgetItem *, bool reset = true,
141 bool target = false);
142 void slotTabChanged (int);
143 void slotClientListDone (int);
144 void slotGeneralOptionsDone (int);
145 void slotButtonLabels (bool);
146 void slotSaveDocument ();
147 void slotSaveExtended ();
148 void slotPasswordContentChanged ();
149 void slotClipboardAttribute ();
150 void slotClipboardElementPath ();
151 void slotClearClipboard ();
152 void slotClipboardTimer ();
153 void slotReleaseTimer();
154 void slotExecuteCommand ();
155 void slotCommandContentChanged ();
156 void slotCommandResultContentChanged ();
157 void slotCommandAnchorClicked (const QUrl &);
158 void slotCommandHelp ();
159 void slotClearCommand ();
160 void slotCommandInquireStateChanged (int);
161 void slotCommandInquireFilename ();
162 void slotSearchCommandResults ();
163 void slotCommandHistoryChanged (int);
164 void slotSaveCommandResult ();
165 void slotEditContent ();
166 void slotCreatePasswordAttribute ();
167 void slotFileOptions ();
168 void slotFileOptionsDone (int);
169 void slotVersion ();
170 void slotCommandResult (PwmdCommandQueueItem *, QString, gpg_error_t, bool);
171 void slotKnownHostCallback (void *data, const char *host, const char *key,
172 size_t len);
173 void slotUpdateConnectTimer ();
174 void slotCancel ();
175 void slotChangeExpire ();
176 void slotItemSelectionChanged ();
177 #ifdef Q_OS_ANDROID
178 void slotAndroidConnectionError (gpg_error_t);
179 void slotAndroidConnectResult (PwmdRemoteHost, int);
180 void slotAndroidConnectReady (PwmdRemoteHost);
181 void slotAndroidAuthenticated ();
182 void slotApplicationStateChanged (Qt::ApplicationState);
183 #endif
184 void slotToggleMultiMode (bool);
185 void showError (gpg_error_t);
187 signals:
188 void knownHostRc (gpg_error_t);
189 void attributesRetrieved (QTreeWidgetItem *);
190 void mainClientLockStateChanged (bool);
192 private:
193 friend class PwmdFileOptionsDialog;
195 typedef struct {
196 QString content;
197 QString name;
198 QString password;
199 QTreeWidgetItem *item;
200 QListWidgetItem *listItem;
201 } AttributeFinalizeT;
203 typedef enum
205 ButtonStateOptions,
206 ButtonStateOpenFile,
207 ButtonStateSave,
208 ButtonStateMain
209 } MainButtonState;
211 friend class PwmdTreeWidget;
212 friend class PwmdListWidget;
213 friend class ApplicationForm;
214 friend class PwmdPlainTextEdit;
216 /* Must match the order of buttons in a QToolBar. See setupToolbars(). */
217 enum {
218 FileToolBarOpen = 0,
219 FileToolBarSave = 1,
220 ElementToolBarCreate = 0,
221 ElementToolBarDelete = 1,
222 ElementToolBarRename = 2,
223 ElementToolBarFind = 3,
224 ContentToolBarInsert = 0,
225 ContentToolBarSave = 1,
226 ContentToolBarPassword = 2,
227 ContentToolBarExpiry = 3,
228 ContentToolBarOk = 5,
229 ContentToolBarRefresh = 6,
230 AttributeToolBarNew = 0,
231 AttributeToolBarDelete = 1,
232 AttributeToolBarOk = 3,
233 AttributeToolBarRefresh = 4,
234 PasswordToolBarInsert = 0,
235 PasswordToolBarSave = 1,
236 PasswordToolBarExpiry = 2,
237 PasswordToolBarOk = 4,
238 PasswordToolBarRefresh = 5,
239 CommandToolBarSave = 0,
240 CommandToolBarClear = 1,
241 CommandToolBarOk = 3,
242 CommandToolBarRefresh = 4,
243 CommandToolBarHelp = 6
246 void fixupEditContentOrAttributes (bool attrs = false);
247 void fixupNextTabOrElement ();
248 void setContent (const QString &, QTreeWidgetItem * = nullptr);
249 void setAttributeContent (const QString &);
250 QString elementPath (const QTreeWidgetItem *item, const QString & sep = "\t");
251 QList < QTreeWidgetItem * >buildElementTree (QByteArray &sexp, bool = true);
252 QList < QListWidgetItem * >buildFilenameTree (gpg_error_t &);
253 bool createAttributeCache (QTreeWidgetItem *, const QString &);
254 bool doSaveContent (bool queue = false, bool save = false,
255 bool exteneded = false, int *which = nullptr,
256 bool force = false);
257 void setAttributeContentOnce (QTreeWidgetItem *item, const QString &path,
258 const QString &value, bool save, bool extended,
259 bool queue);
260 void setBusy (bool b = true, int cmdId = PwmdCmdIdInvalid);
261 void setConnected (bool = true, Pwmd::ConnectionState = Pwmd::Connected);
262 void saveDocument (bool exteneded = false);
263 bool doFinished (bool &no, bool ignore = false);
264 bool refreshElementTree (bool target = false);
265 void refreshElementTreeFinalize (QByteArray &);
266 bool editElements ();
267 bool hasBadTarget (QTreeWidgetItem *, const QString &, const QString &);
268 bool badTarget (const QTreeWidgetItem *);
269 bool targetLoop (const QTreeWidgetItem *);
270 QTreeWidgetItem *findElement (const QString &, QTreeWidgetItem * = 0,
271 bool = false, PwmdTreeWidget * = nullptr);
272 void elementSelected (QTreeWidgetItem *, bool refresh = false,
273 QTreeWidgetItem * = 0);
274 void elementSelectedFinalize (QTreeWidgetItem *, const QString &,
275 bool attrs = false);
276 void sortAttributes ();
277 bool refreshAttributeList (QTreeWidgetItem *, bool force = false,
278 bool queue = false, bool fromTree = false);
279 void refreshAttributeListFinalize (QTreeWidgetItem *, const QString &,
280 bool create = true, bool fromTree = false);
281 bool hasTarget (const QTreeWidgetItem *, bool parent = false);
282 QString elementName (const QTreeWidgetItem *);
283 void setItemText (QTreeWidgetItem *, const QString &, bool hasTarget = false);
284 void tabify (QPlainTextEdit *);
285 void setOpen (bool = true);
286 void popupContextMenu ();
287 void resetSelectedItems ();
288 bool isParentOf (QTreeWidgetItem *, QTreeWidgetItem *);
289 void setModified (bool = true);
290 void setModifiedContent (bool b = true);
291 void setFileModified (bool = true);
292 QString updateSocket ();
293 void createNewElement (bool root = false, bool sibling = false);
294 bool event (QEvent *);
295 QTreeWidgetItem *resolveTargets (QTreeWidgetItem *);
296 QTreeWidgetItem *resolveElementPath (QTreeWidgetItem *);
297 void setAttribute (QTreeWidgetItem *, const QString &, const QString & = 0,
298 bool update = false);
299 void removeAttribute (QTreeWidgetItem *, const QString &,
300 bool update = false);
301 bool deleteAttribute (QTreeWidgetItem *, const QString &name, bool queuequeue = false);
302 void deleteAttributeFinalize (QTreeWidgetItem *, const QString &);
303 void setMainButtons (MainButtonState);
304 void setTimeLabels (QTreeWidgetItem *, bool needsRefresh = false);
305 void clearElementTree (QTreeWidget *);
306 gpg_error_t doDndCopyElement (const QString & src, const QString & dst,
307 bool queue = false);
308 void dndCopyMoveElementFinalize (const QString & = 0);
309 void dndCreateTargetFinalize ();
310 void updateContentPointer (QTreeWidgetItem *, PwmdElementContent *);
311 void editAttributes (bool resolve = false, QTreeWidgetItem * = 0);
312 QString toBase64 (QTreeWidgetItem *, QByteArray &);
313 bool setNewAttribute (QTreeWidgetItem *, QString, QString = 0, bool fromSlot = false, bool queue = false);
314 void setNewAttributeFinalize (AttributeFinalizeT *, bool fromSlot = false);
315 void updateSocketOptions ();
316 void updateCacheTimeout (const QString &);
317 void toggleOtherTabs (bool);
318 bool isElementOwner (QTreeWidgetItem *, bool secondary = false);
319 bool hasOwnerOfItemSelected ();
320 static gpg_error_t badPassphraseCallback (void *, bool);
321 void setElementStatusTip (QTreeWidgetItem *);
322 QString clientState (unsigned);
323 void updateStateForClient (QString);
324 void clearClientList (QTreeWidget *);
325 void readConfigSettings (QString &filename, QString &sock, bool init = true);
326 void closeEvent (QCloseEvent *);
327 bool confirmQuit ();
328 void setupToolbars ();
329 void setupToolBarLabels (bool b);
330 void setElementItemsEnabled (QTreeWidgetItem *, bool, QTreeWidgetItem * = 0);
331 void setContentItemsEnabled (bool = true, bool = true, QTreeWidgetItem * = 0);
332 void setPasswordItemsEnabled (bool = true, QTreeWidgetItem * = 0);
333 void setAttributeItemsEnabled (bool = true, QTreeWidgetItem * = 0);
334 void setCommandItemsEnabled (bool = true, QTreeWidgetItem * = 0);
335 void clearAttributeTab ();
336 void setCommandInquireFilename (const QString &);
337 bool fixupToolBar (QToolBar *, int, bool enable, bool test = false,
338 QWidget ** = 0);
339 bool fixupMenu (QMenu *, int which, bool enable, int checked = -1,
340 bool test = false);
341 void fixupMultiToolbar ();
342 void ignoreErrors (PwmdCommandQueueItem *, bool = false);
343 bool needsUpdate ();
344 void cacheContentToggled ();
345 void saveAttrContentFinalize (const QString &path, AttributeFinalizeT *);
346 void saveContentFinalize (QTreeWidgetItem *, const QString &content,
347 bool password);
348 void savePasswordContentFinalize (QTreeWidgetItem *, const QString &type);
349 void renameElementFinalize ();
350 void newElementFinalize (QString path);
351 void deleteElementFinalize (PwmdCommandQueueItem *);
352 void invokingUserFinalize (const QString &);
353 void currentUserFinalize (const QString &);
354 void reloadFinalize ();
355 void openFileFinalize (gpg_error_t);
356 void openFormFinalize (QByteArray &, const QString &, const QString &);
357 void setWindowTitle (bool);
358 void updateConnectTimer (bool reset = false, bool cancel = false);
359 void updatePasswordAttribute (QTreeWidgetItem *, bool queue = false);
360 bool isInvoker ();
361 void updateElementExpiry (long);
362 void setElementExpireLabel (long);
363 void updateTargetAttributes (QTreeWidgetItem *item, const QString &name,
364 const QString &value, bool remove = false);
365 bool attributeIsEditable (QTreeWidgetItem *, QListWidgetItem *);
366 bool isReservedAttribute (const QString &);
367 bool isProtectedAttribute (const QString &);
368 int previousAttributeIndex ();
369 void updateBusyStatusBar (Pwmd::ConnectionState);
370 QTreeWidgetItem *findElementText (const QString &str,
371 bool elements = true,
372 bool attrs = false,
373 bool attrValues = false);
374 void findElementCommon (bool previous);
375 void setCurrentElement (QTreeWidgetItem *);
376 void saveExpandedItemList ();
377 void setClipboard (const QString &);
378 bool hasQueuedRefreshTree ();
379 bool hasMultiSelection ();
380 bool hasSingleSelection ();
381 QTreeWidgetItem *firstSelectedItem ();
383 Pwmd *pwm;
384 Pwmd::ConnectionState state;
385 QString _filename;
386 QString _socket;
387 Ui::PwmdMainWindow ui;
388 QTreeWidgetItem *attributeElement;
389 QTreeWidgetItem *selectedElement;
390 QTreeWidgetItem *previousElement;
391 QTreeWidgetItem *targetAttributeElement;
392 QTreeWidgetItem *copyElement;
393 QTreeWidgetItem *oldSelectedItem;
394 QColor previousElementColor;
395 QColor previousElementColorBg;
396 QString selectedElementPath;
397 QTreeWidgetItem *dropElement;
398 QTreeWidgetItem *newElement;
399 QListWidgetItem *selectedAttribute;
400 QListWidgetItem *newAttribute;
401 QListWidgetItem *newFilename;
402 QString selectedFilename;
403 unsigned iterationsOrig;
404 PwmdRemoteHost currentHostData;
405 bool importRoot;
406 QWidget *previousRightPage;
407 QColor targetColor;
408 QColor invalidTargetColor;
409 QColor hilightColor;
410 QColor targetLoopColor;
411 QColor permissionsColor;
412 QColor targetColorBg;
413 QColor invalidTargetColorBg;
414 QColor hilightColorBg;
415 QColor targetLoopColorBg;
416 QColor permissionsColorBg;
417 bool sortElementTree;
418 QString clientName;
419 int dndOperation;
420 int ipVersion;
421 QTimer *clipboardTimer;
422 QTimer *releaseTimer;
423 QClipboard *clipboard;
424 unsigned clipboardTimeout;
425 bool newFile;
426 QStringList connectParameters;
427 bool editTargetAttributes;
428 unsigned features; // pwmd_features()
429 int pinentry_try;
430 MainButtonState mainButtonState;
431 QStringList invokingUser;
432 QString connectedUser;
433 PwmdClientDialog *clientDialog;
434 PwmdOptionsDialog *optionsDialog;
435 QToolBar *fileToolBar;
436 QToolBar *editToolBar;
437 QToolBar *elementToolBar;
438 QToolBar *contentToolBar;
439 QToolBar *passwordToolBar;
440 QToolBar *attributeToolBar;
441 QToolBar *commandToolBar;
442 bool cacheContent;
443 int lockTimeout;
444 QProgressBar *progressBar;
445 PwmdFileOptionsDialog *fileOptionsDialog;
446 PwmdOpenDialog *openDialog;
447 QPushButton *cancelButton;
448 QString previousAttributeText;
449 bool isBusy;
450 QString commandInquireFilename;
451 QAction *findElementAction;
452 QWidget *findElementWidget;
453 unsigned nSelectedElements;
454 bool searchAndReplace;
455 PwmdSearchDialog *advancedSearchDialog;
456 QLabel *searchResultStatusBarLabel;
457 #ifdef Q_OS_ANDROID
458 bool androidAuthenticating;
459 #endif
461 typedef enum {
462 DndNone, DndCopy, DndMove, DndTarget, DndAny
463 } DndOperation;
466 class AttributesRetrievedEvent : public QEvent
468 public:
469 AttributesRetrievedEvent (QEvent::Type t) : QEvent (t)
471 _item = nullptr;
474 void setItem (QTreeWidgetItem *item)
476 _item = item;
479 QTreeWidgetItem *item ()
481 return _item;
484 private:
485 QTreeWidgetItem *_item;
488 class PwmdPasswordOnlyValidate:public QValidator
490 Q_OBJECT
491 public:
492 State validate (QString & in, int &pos) const
494 (void) pos;
496 for (int i = 0; i < in.length (); i++)
498 if (in.count (in.at (i)) > 1)
499 return Invalid;
502 return Acceptable;
506 class PwmdCommandHistoryData
508 public:
509 PwmdCommandHistoryData (QString args, bool inquire = false,
510 QString inquireCmd = 0, QString inquireFilename = 0)
512 _args = args;
513 _inquire = inquire;
514 _inquireCmd = inquireCmd;
515 _inquireFilename = inquireFilename;
517 ~PwmdCommandHistoryData () {};
519 bool isInquire ()
521 return _inquire;
524 const QString &inquireCmd ()
526 return _inquireCmd;
529 const QString &args ()
531 return _args;
534 const QString &inquireFilename ()
536 return _inquireFilename;
539 private:
540 bool _inquire;
541 QString _args;
542 QString _inquireCmd;
543 QString _inquireFilename;
546 Q_DECLARE_METATYPE (PwmdCommandHistoryData *);
548 class SearchResult
550 public:
551 enum {
552 None = 0x0,
553 Element = 0x1,
554 Attr = 0x2,
555 AttrValue = 0x4
556 } SearchResultType;
558 SearchResult (QTreeWidgetItem *e, int type, QList <int>attrs)
560 _element = e;
561 _attrs = attrs;
562 _curAttr = reset ();
563 _type = type;
565 ~SearchResult () {};
567 const QList <int> attrs ()
569 return _attrs;
572 int resultType ()
574 return _type;
577 QTreeWidgetItem *element ()
579 return _element;
582 int nextAttr ()
584 int n = _attrs.indexOf (curAttr ());
585 if (n < 0 || n+1 >= _attrs.count ())
587 _curAttr = -1;
588 return curAttr ();
591 _curAttr = _attrs.at (n+1);
592 return curAttr ();
595 int curAttr ()
597 return _curAttr;
600 int lastAttr ()
602 if (_attrs.count () == 0)
603 return -1;
605 _curAttr = _attrs.last ();
606 return curAttr ();
609 int prevAttr ()
611 int n = _attrs.indexOf (curAttr ());
612 if (n < 0 || n-1 < 0)
614 _curAttr = -1;
615 return curAttr ();
618 _curAttr = _attrs.at (n-1);
619 return curAttr ();
622 int reset ()
624 _curAttr = _attrs.count () ? _attrs.at (0) : -1;
625 return curAttr ();
628 private:
629 QTreeWidgetItem *_element;
630 QList <int>_attrs;
631 int _curAttr;
632 int _type;
635 #endif