2 Copyright (C) 2010-2024 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
21 #ifndef PWMDMAINWINDOW_H
22 #define PWMDMAINWINDOW_H
24 #include <QMainWindow>
25 #include <QTreeWidget>
26 #include <QListWidget>
28 #include <QStringList>
31 #include <QProgressBar>
32 #include <QPushButton>
36 #include "pwmdRemoteHost.h"
37 #include "pwmdClientDialog.h"
38 #include "pwmdOptionsDialog.h"
39 #include "pwmdFileOptionsDialog.h"
40 #include "pwmdOpenDialog.h"
41 #include "pwmdSearchDialog.h"
43 #include "pwmdAndroidJNI.h"
45 #include "ui_pwmdMainWindow.h"
48 class PwmdFileOptionsWidget
;
50 class PwmdMainWindow
: public QMainWindow
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);
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.
70 // Returns the URL of the connected socket.
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);
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);
170 void slotCommandResult (PwmdCommandQueueItem
*, QString
, gpg_error_t
, bool);
171 void slotKnownHostCallback (void *data
, const char *host
, const char *key
,
173 void slotUpdateConnectTimer ();
175 void slotChangeExpire ();
176 void slotItemSelectionChanged ();
178 void slotAndroidConnectionError (gpg_error_t
);
179 void slotAndroidConnectResult (PwmdRemoteHost
, int);
180 void slotAndroidConnectReady (PwmdRemoteHost
);
181 void slotAndroidAuthenticated ();
182 void slotApplicationStateChanged (Qt::ApplicationState
);
184 void slotToggleMultiMode (bool);
185 void showError (gpg_error_t
);
188 void knownHostRc (gpg_error_t
);
189 void attributesRetrieved (QTreeWidgetItem
*);
190 void mainClientLockStateChanged (bool);
193 friend class PwmdFileOptionsDialog
;
199 QTreeWidgetItem
*item
;
200 QListWidgetItem
*listItem
;
201 } AttributeFinalizeT
;
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(). */
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,
257 void setAttributeContentOnce (QTreeWidgetItem
*item
, const QString
&path
,
258 const QString
&value
, bool save
, bool extended
,
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
&,
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
,
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
*);
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,
339 bool fixupMenu (QMenu
*, int which
, bool enable
, int checked
= -1,
341 void fixupMultiToolbar ();
342 void ignoreErrors (PwmdCommandQueueItem
*, bool = false);
344 void cacheContentToggled ();
345 void saveAttrContentFinalize (const QString
&path
, AttributeFinalizeT
*);
346 void saveContentFinalize (QTreeWidgetItem
*, const QString
&content
,
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);
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,
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 ();
384 Pwmd::ConnectionState state
;
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
;
406 QWidget
*previousRightPage
;
408 QColor invalidTargetColor
;
410 QColor targetLoopColor
;
411 QColor permissionsColor
;
412 QColor targetColorBg
;
413 QColor invalidTargetColorBg
;
414 QColor hilightColorBg
;
415 QColor targetLoopColorBg
;
416 QColor permissionsColorBg
;
417 bool sortElementTree
;
421 QTimer
*clipboardTimer
;
422 QTimer
*releaseTimer
;
423 QClipboard
*clipboard
;
424 unsigned clipboardTimeout
;
426 QStringList connectParameters
;
427 bool editTargetAttributes
;
428 unsigned features
; // pwmd_features()
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
;
444 QProgressBar
*progressBar
;
445 PwmdFileOptionsDialog
*fileOptionsDialog
;
446 PwmdOpenDialog
*openDialog
;
447 QPushButton
*cancelButton
;
448 QString previousAttributeText
;
450 QString commandInquireFilename
;
451 QAction
*findElementAction
;
452 QWidget
*findElementWidget
;
453 unsigned nSelectedElements
;
454 bool searchAndReplace
;
455 PwmdSearchDialog
*advancedSearchDialog
;
456 QLabel
*searchResultStatusBarLabel
;
458 bool androidAuthenticating
;
462 DndNone
, DndCopy
, DndMove
, DndTarget
, DndAny
466 class AttributesRetrievedEvent
: public QEvent
469 AttributesRetrievedEvent (QEvent::Type t
) : QEvent (t
)
474 void setItem (QTreeWidgetItem
*item
)
479 QTreeWidgetItem
*item ()
485 QTreeWidgetItem
*_item
;
488 class PwmdPasswordOnlyValidate
:public QValidator
492 State
validate (QString
& in
, int &pos
) const
496 for (int i
= 0; i
< in
.length (); i
++)
498 if (in
.count (in
.at (i
)) > 1)
506 class PwmdCommandHistoryData
509 PwmdCommandHistoryData (QString args
, bool inquire
= false,
510 QString inquireCmd
= 0, QString inquireFilename
= 0)
514 _inquireCmd
= inquireCmd
;
515 _inquireFilename
= inquireFilename
;
517 ~PwmdCommandHistoryData () {};
524 const QString
&inquireCmd ()
529 const QString
&args ()
534 const QString
&inquireFilename ()
536 return _inquireFilename
;
543 QString _inquireFilename
;
546 Q_DECLARE_METATYPE (PwmdCommandHistoryData
*);
558 SearchResult (QTreeWidgetItem
*e
, int type
, QList
<int>attrs
)
567 const QList
<int> attrs ()
577 QTreeWidgetItem
*element ()
584 int n
= _attrs
.indexOf (curAttr ());
585 if (n
< 0 || n
+1 >= _attrs
.count ())
591 _curAttr
= _attrs
.at (n
+1);
602 if (_attrs
.count () == 0)
605 _curAttr
= _attrs
.last ();
611 int n
= _attrs
.indexOf (curAttr ());
612 if (n
< 0 || n
-1 < 0)
618 _curAttr
= _attrs
.at (n
-1);
624 _curAttr
= _attrs
.count () ? _attrs
.at (0) : -1;
629 QTreeWidgetItem
*_element
;