/applicationFormFinalizePage.cpp
C++ | 461 lines | 379 code | 57 blank | 25 comment | 71 complexity | 30aba0234115033586223bdb10d04b8b MD5 | raw file
Possible License(s): LGPL-2.1
- /*
- Copyright (C) 2013-2020 Ben Kibbey <bjk@luxsci.net>
- This file is part of qpwmc.
- This library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 2.1 of the License, or (at your option) any later version.
- This library is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
- You should have received a copy of the GNU Lesser General Public
- License along with this library; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
- USA
- */
- #include <QTimer>
- #include <QCalendarWidget>
- #include <QDateTime>
- #include "pwmdMainWindow.h"
- #include "applicationFormFinalizePage.h"
- #include "applicationForm.h"
- #include "applicationFormXmlHandler.h"
- #include "applicationFormDateSelector.h"
- #include "cmdIds.h"
- ApplicationFormFinalizePage::ApplicationFormFinalizePage (QWidget *p)
- : QWizardPage (p)
- {
- ui.setupUi (this);
- pwm = NULL;
- newFile = false;
- filename = QString ();
- ownHandle = true;
- saving = false;
- }
- void
- ApplicationFormFinalizePage::slotBusy (int cmdId, bool b)
- {
- (void)cmdId;
- if (b)
- setCursor (Qt::WaitCursor);
- else
- unsetCursor ();
- }
- void
- ApplicationFormFinalizePage::slotKnownHostCallback (void *data,
- const char *host,
- const char *key, size_t len)
- {
- gpg_error_t rc = Pwmd::knownHostPrompt (data, host, key, len);
- ApplicationForm *w = qobject_cast<ApplicationForm *> (wizard ());
- w->setHasError (rc != 0);
- emit knownHostRc (rc);
- }
- void
- ApplicationFormFinalizePage::commit ()
- {
- ApplicationForm *form = static_cast<ApplicationForm *>(wizard ());
- bool notFound = false;
- wizard ()->setOptions (QWizard::NoCancelButton|QWizard::NoBackButtonOnLastPage);
- QAbstractButton *pb = wizard ()->button (QWizard::FinishButton);
- pb->setEnabled (false);
- foreach (ApplicationFormWidget *w, form->handler->widgets ())
- {
- QString path = QString ();
- if (w->id () == "pwmdRootElement")
- continue;
- if (!w->childOf ().isEmpty ())
- {
- path = form->buildElementPath (path, w->childOf (), notFound);
- if (notFound)
- break;
- }
- if (!w->pwmdName ().isEmpty ())
- {
- if (!path.isEmpty ())
- path += "\t";
- path += w->pwmdName ();
- path.append ("\t");
- if (!w->dateSelector () && !w->value ().isEmpty ())
- path.append (w->value ());
- }
- else if (!path.isEmpty ())
- path.append ("\t");
- if (w->dateSelector () && !w->value ().isEmpty ())
- {
- ApplicationFormDateSelector *d = qobject_cast<ApplicationFormDateSelector *> (w->widget ());
- path += d->date ().toString (w->value ());
- if (!w->value ().endsWith ("<TAB>"))
- path += "<TAB>";
- }
- path = path.replace ("<TAB>", "\t");
- if (path.isEmpty ())
- continue;
- ApplicationElement *ae = new ApplicationElement (path, w->isHidden ());
- ae->setAttrs (w->attrs ());
- elements.append (ae);
- }
- /* Remove paths that begin with elements from other paths to prevent
- redundant element creation. */
- for (int i = 0; !notFound && i < elements.count (); i++)
- {
- ApplicationElement *e = elements.at (i);
- for (int n = 0; n < elements.count (); n++)
- {
- ApplicationElement *x = elements.at (n);
- if (e->path ().startsWith (x->path ()) && x != e)
- {
- elements.removeAt (n);
- delete x;
- break;
- }
- }
- }
- ui.progressBar->setMaximum (elements.count());
- filename = form->buildElementPath (QString (), "pwmdFilename", notFound);
- QString sock = form->buildElementPath (QString (), "pwmdSocket", notFound);
- qRegisterMetaType <Pwmd::ConnectionState>("Pwmd::ConnectionState");
- qRegisterMetaType <gpg_error_t>("gpg_error_t");
- if (!pwm)
- {
- ownHandle = true;
- pwm = new Pwmd (filename, "qpwmc", sock, this);
- pwm->setData (sock.toUtf8 ().data ());
- pwm->setLockOnOpen (true);
- connect (pwm, SIGNAL (stateChanged (Pwmd::ConnectionState)),
- this, SLOT (slotConnectionStateChanged (Pwmd::ConnectionState)));
- connect (pwm, SIGNAL (statusMessage (QString, void *)), this,
- SLOT (slotStatusMessage (QString, void *)));
- connect (pwm, SIGNAL (knownHost (void *, const char *, const char *, size_t)), this, SLOT (slotKnownHostCallback (void *, const char *, const char *, size_t)));
- if (PwmdRemoteHost::fillRemoteHost (sock, hostData))
- {
- pwm->setSocket (PwmdRemoteHost::socketUrl (hostData));
- pwm->setConnectParameters (hostData.socketArgs ());
- pwmd_setopt (pwm->handle (), PWMD_OPTION_SSH_AGENT,
- hostData.sshAgent ());
- pwmd_setopt (pwm->handle (), PWMD_OPTION_SSH_NEEDS_PASSPHRASE,
- hostData.sshNeedsPassphrase ());
- pwmd_setopt (pwm->handle (), PWMD_OPTION_SOCKET_TIMEOUT,
- hostData.socketTimeout ());
- pwmd_setopt (pwm->handle (), PWMD_OPTION_TLS_VERIFY,
- hostData.tlsVerify ());
- if (!hostData.tlsPriority().isEmpty ())
- pwmd_setopt (pwm->handle (), PWMD_OPTION_TLS_PRIORITY,
- hostData.tlsPriority ().toUtf8 ().data ());
- }
- }
- disconnect (pwm, SIGNAL (busy (int, bool)), this, SLOT (slotBusy (int, bool)));
- connect (pwm, SIGNAL (busy (int, bool)), this, SLOT (slotBusy (int, bool)));
- disconnect (pwm, SIGNAL (commandResult (PwmdCommandQueueItem *, QString, gpg_error_t, bool)), this, SLOT (slotCommandResult (PwmdCommandQueueItem *, QString, gpg_error_t, bool)));
- connect (pwm, SIGNAL (commandResult (PwmdCommandQueueItem *, QString, gpg_error_t, bool)), this, SLOT (slotCommandResult (PwmdCommandQueueItem *, QString, gpg_error_t, bool)));
- if (!notFound)
- {
- if (pwm->state () == Pwmd::Init)
- pwm->connect (0, 0, true);
- if (ownHandle)
- message (QString (tr ("Connecting to %1...")).arg (pwm->socket ()));
- if (pwm->state () == Pwmd::Connected || pwm->state () == Pwmd::Init)
- {
- PwmdInquireData *inq = new PwmdInquireData (pwm->handle(), pwm->filename());
- pwm->open (Pwmd::inquireCallback, inq);
- return;
- }
- openFileFinalize ();
- }
- }
- void
- ApplicationFormFinalizePage::attrStoreFinalize (ApplicationElement *e,
- gpg_error_t &rc, bool isFinal)
- {
- (void)rc;
- if (isFinal)
- connectionProgress (elements.indexOf (e));
- }
- void
- ApplicationFormFinalizePage::getContentFinalize (ApplicationElement *e,
- gpg_error_t &rc, bool queued)
- {
- QString attrName;
- QString attrValue;
- bool tab = e->path ().endsWith ("\t");
- QStringList l = e->path ().split ("\t");
- if (tab || e->isHidden ())
- l.removeLast ();
- QString s = QString (tr ("Creating path: %1")).arg (l.join ("->"));
- if (!rc)
- {
- message (s);
- message (tr ("Not overwriting existing content!"));
- connectionProgress (elements.indexOf (e));
- return;
- }
- else if (gpg_err_code (rc) == GPG_ERR_ELEMENT_NOT_FOUND
- || gpg_err_code (rc) == GPG_ERR_NO_DATA)
- rc = 0;
- if (rc)
- {
- if (!queued)
- {
- message (s);
- message (QString ("ERR %1: %2").arg (rc).arg (gpg_strerror (rc)));
- }
- ApplicationForm *w = qobject_cast<ApplicationForm *> (wizard ());
- w->setHasError ();
- connectionProgress (elements.indexOf (e));
- return;
- }
- message (s);
- PwmdInquireData *inq = new PwmdInquireData (e->path ());
- inq->setUserPtr (e);
- inq->setUserBool (!e->attrs ().count ()); // final command for element
- pwm->command (new PwmdCommandQueueItem (PwmdCmdIdFormStoreContent, "STORE",
- Pwmd::inquireCallback, inq), true);
- for (int i = 0; i < e->attrs ().count (); i++)
- {
- ApplicationFormAttr *a = e->attrs ().at (i);
- QString args = QString ("SET %1 %2").arg (a->name ()).arg (e->pathNoContent ());
- if (!a->value ().isEmpty ())
- args.append (QString (" %1").arg (a->value ()));
- inq = new PwmdInquireData (args);
- inq->setUserPtr (e);
- pwm->command (new PwmdCommandQueueItem (PwmdCmdIdFormAttrNew, "ATTR",
- Pwmd::inquireCallback, inq), true);
- if (i+1 == e->attrs ().count ())
- inq->setUserBool (true); // final command for element
- }
- pwm->runQueue ();
- }
- void
- ApplicationFormFinalizePage::openFileFinalize ()
- {
- for (int i = 0; i < elements.count (); i++)
- {
- ApplicationElement *e = elements.at (i);
- /* To be on the safe side we will never overwrite the content of an
- element since it may be shared with another element. The exception
- is if the element exists but the content is empty. */
- PwmdInquireData *inq = new PwmdInquireData (e->pathNoContent ());
- inq->setUserPtr (e);
- PwmdCommandQueueItem *item = new PwmdCommandQueueItem (PwmdCmdIdFormGetContent,
- "GET",
- Pwmd::inquireCallback,
- inq);
- item->addError (GPG_ERR_SOURCE_USER_1, GPG_ERR_ELEMENT_NOT_FOUND);
- pwm->command (item, true);
- }
- pwm->runQueue ();
- }
- ApplicationFormFinalizePage::~ApplicationFormFinalizePage ()
- {
- while (elements.count ())
- {
- ApplicationElement *e = elements.takeFirst ();
- delete e;
- }
- elements.clear ();
- }
- void
- ApplicationFormFinalizePage::slotStatusMessage (QString s, void *data)
- {
- (void)data;
- if (s == "NEWFILE")
- newFile = true;
- }
- void
- ApplicationFormFinalizePage::slotConnectionStateChanged (Pwmd::ConnectionState s)
- {
- switch (s)
- {
- case Pwmd::Init:
- pwmd_setopt (pwm->handle (), PWMD_OPTION_SOCKET_TIMEOUT,
- hostData.connectTimeout ());
- pwmd_setopt (pwm->handle (), PWMD_OPTION_SSH_AGENT,
- hostData.sshAgent ());
- pwmd_setopt (pwm->handle (), PWMD_OPTION_SSH_NEEDS_PASSPHRASE,
- hostData.sshNeedsPassphrase ());
- pwmd_setopt (pwm->handle (), PWMD_OPTION_TLS_VERIFY,
- hostData.tlsVerify ());
- if (!hostData.tlsPriority().isEmpty ())
- pwmd_setopt (pwm->handle (), PWMD_OPTION_TLS_PRIORITY,
- hostData.tlsPriority ().toUtf8 ().data ());
- pwmd_setopt (pwm->handle (), PWMD_OPTION_SOCKET_TIMEOUT,
- hostData.connectTimeout ());
- {
- QAbstractButton *pb = wizard ()->button (QWizard::FinishButton);
- pb->setEnabled (true);
- }
- break;
- case Pwmd::Connected:
- message (QString (tr ("Opening data file %1...")).arg (filename));
- pwmd_setopt (pwm->handle (), PWMD_OPTION_SOCKET_TIMEOUT,
- hostData.socketTimeout ());
- break;
- default:
- break;
- }
- }
- void
- ApplicationFormFinalizePage::connectionProgress (int i)
- {
- ui.progressBar->setValue (i+1);
-
- if (!saving && ui.progressBar->value () == ui.progressBar->maximum())
- {
- saving = true;
- save ();
- }
- }
- void
- ApplicationFormFinalizePage::message (const QString &s)
- {
- QString str = ui.textEdit->document ()->toPlainText ();
- ui.textEdit->document ()->setPlainText (QString (str + s + "\n").replace ("\t", "<TAB>"));
- }
- void
- ApplicationFormFinalizePage::slotCommandResult (PwmdCommandQueueItem *item,
- QString result, gpg_error_t rc,
- bool queued)
- {
- (void)result;
- PwmdInquireData *inq = item->data ();
- ApplicationForm *w = qobject_cast<ApplicationForm *> (wizard ());
- switch (item->id ())
- {
- case PwmdCmdIdInternalPassword:
- if (rc)
- w->setHasError ();
- item->setSeen ();
- return;
- case PwmdCmdIdInternalStatusError:
- if (ownHandle) // our own pwm handle
- {
- message (QString (tr ("Connection error: %1")).arg (gpg_strerror (rc)));
- QAbstractButton *pb = wizard ()->button (QWizard::FinishButton);
- pb->setEnabled (true);
- }
- break;
- case PwmdCmdIdInternalConnect:
- if (rc)
- {
- if (gpg_err_code (rc) == GPG_ERR_ETIMEDOUT)
- pwm->reset ();
- message (tr ("Failed to connect."));
- }
- break;
- case PwmdCmdIdInternalOpen:
- if (!rc)
- openFileFinalize ();
- break;
- case PwmdCmdIdInternalSave:
- if (rc)
- message (tr ("Failed to save."));
- else
- message (tr ("Save succeeded."));
- break;
- case PwmdCmdIdFormGetContent:
- getContentFinalize (static_cast <ApplicationElement *> (inq->userPtr()), rc, queued);
- break;
- case PwmdCmdIdFormStoreContent:
- attrStoreFinalize (static_cast <ApplicationElement *> (inq->userPtr()), rc, inq->userBool ());
- break;
- case PwmdCmdIdFormAttrNew:
- attrStoreFinalize (static_cast <ApplicationElement *> (inq->userPtr()), rc, inq->userBool ());
- default:
- break;
- }
- if (rc && !item->checkError (rc) && !queued)
- w->setHasError ();
- item->setSeen ();
- }
- void
- ApplicationFormFinalizePage::save ()
- {
- if (ownHandle)
- {
- message (tr ("Saving ..."));
- PwmdSaveDialog *d = new PwmdSaveDialog (pwm, newFile);
- PwmdSaveWidget *w = d->widget ();
- if (newFile)
- {
- if (d->exec ())
- message (tr ("Cancelled."));
- }
- else
- w->save (0, true);
- delete d;
- }
- else
- {
- ApplicationForm *w = qobject_cast<ApplicationForm *> (wizard ());
- message (w->hasError () ? tr ("Failed.") : tr ("Done!"));
- }
- QAbstractButton *pb = wizard ()->button (QWizard::FinishButton);
- pb->setEnabled (true);
- }
- void
- ApplicationFormFinalizePage::setHandle (Pwmd *h)
- {
- pwm = h;
- ownHandle = false;
- }