1 /*
2  * LibrePCB - Professional EDA for everyone!
3  * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4  * https://librepcb.org/
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 /*******************************************************************************
21  *  Includes
22  ******************************************************************************/
23 #include "directorylockhandlerdialog.h"
24 
25 #include "ui_directorylockhandlerdialog.h"
26 
27 #include <QtCore>
28 #include <QtWidgets>
29 
30 /*******************************************************************************
31  *  Namespace
32  ******************************************************************************/
33 namespace librepcb {
34 
35 /*******************************************************************************
36  *  Constructors / Destructor
37  ******************************************************************************/
38 
DirectoryLockHandlerDialog(const FilePath & directory,const QString & user,bool allowOverrideLock,QWidget * parent)39 DirectoryLockHandlerDialog::DirectoryLockHandlerDialog(
40     const FilePath& directory, const QString& user, bool allowOverrideLock,
41     QWidget* parent) noexcept
42   : QDialog(parent), mUi(new Ui::DirectoryLockHandlerDialog) {
43   mUi->setupUi(this);
44   mUi->lblDescription->setText(
45       mUi->lblDescription->text().arg(directory.toNative(), user));
46   mUi->lblDisclaimer->setVisible(allowOverrideLock);
47 
48   if (allowOverrideLock) {
49     // Add "accept risk" checkbox to buttonbox.
50     QCheckBox* cbxAcceptRisk = new QCheckBox(tr("I accept the risk."), this);
51     mUi->buttonBox->addButton(cbxAcceptRisk, QDialogButtonBox::ActionRole);
52 
53     // Add "override lock" button to buttonbox.
54     QPushButton* btnOverride = mUi->buttonBox->addButton(
55         tr("Open anyway"), QDialogButtonBox::DestructiveRole);
56     btnOverride->setEnabled(false);
57     connect(btnOverride, &QPushButton::clicked, this, &QDialog::accept);
58     connect(cbxAcceptRisk, &QCheckBox::toggled, btnOverride,
59             &QPushButton::setEnabled);
60   }
61 
62   adjustSize();
63 }
64 
~DirectoryLockHandlerDialog()65 DirectoryLockHandlerDialog::~DirectoryLockHandlerDialog() noexcept {
66 }
67 
68 /*******************************************************************************
69  *  Public Methods
70  ******************************************************************************/
71 
72 DirectoryLock::LockHandlerCallback
createDirectoryLockCallback(QWidget * parent)73     DirectoryLockHandlerDialog::createDirectoryLockCallback(
74         QWidget* parent) noexcept {
75   return [parent](const FilePath& p, DirectoryLock::LockStatus status,
76                   const QString& user) {
77     bool allowOverride =
78         (status == DirectoryLock::LockStatus::LockedByOtherUser) ||
79         (status == DirectoryLock::LockStatus::LockedByUnknownApp);
80     DirectoryLockHandlerDialog dialog(p, user, allowOverride, parent);
81     if (dialog.exec() == QDialog::Accepted) {
82       return true;
83     }
84     throw UserCanceled(__FILE__, __LINE__);
85   };
86 }
87 
88 /*******************************************************************************
89  *  End of File
90  ******************************************************************************/
91 
92 }  // namespace librepcb
93