1 /* This file is part of the KDE project
2    Copyright (C) 20079 Dag Andersen <danders@get2net.dk>
3 
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public
6    License as published by the Free Software Foundation; either
7    version 2 of the License, or (at your option) any later version.
8 
9    This library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Library General Public License for more details.
13 
14    You should have received a copy of the GNU Library General Public License
15    along with this library; see the file COPYING.LIB.  If not, write to
16    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18 */
19 
20 // clazy:excludeall=qstring-arg
21 #include "kptinsertfiledlg.h"
22 #include "kptnode.h"
23 #include "kptproject.h"
24 
25 #include <KLocalizedString>
26 #include <KIO/StatJob>
27 
28 namespace KPlato
29 {
30 
InsertFileDialog(Project & project,Node * currentNode,QWidget * parent)31 InsertFileDialog::InsertFileDialog(Project &project, Node *currentNode, QWidget *parent)
32     : KoDialog(parent)
33 {
34     setCaption(i18n("Insert File"));
35     setButtons(KoDialog::Ok | KoDialog::Cancel);
36     setDefaultButton(Ok);
37     showButtonSeparator(true);
38 
39     m_panel = new InsertFilePanel(project, currentNode, this);
40 
41     setMainWidget(m_panel);
42 
43     enableButtonOk(false);
44 
45     connect(m_panel, &InsertFilePanel::enableButtonOk, this, &KoDialog::enableButtonOk);
46 }
47 
url() const48 QUrl InsertFileDialog::url() const
49 {
50     return m_panel->url();
51 }
52 
parentNode() const53 Node *InsertFileDialog::parentNode() const
54 {
55     return m_panel->parentNode();
56 }
57 
afterNode() const58 Node *InsertFileDialog::afterNode() const
59 {
60     return m_panel->afterNode();
61 }
62 
63 //------------------------
InsertFilePanel(Project & project,Node * currentNode,QWidget * parent)64 InsertFilePanel::InsertFilePanel(Project &project, Node *currentNode, QWidget *parent)
65     : QWidget(parent),
66     m_project(project),
67     m_node(currentNode)
68 {
69     ui.setupUi(this);
70 
71     if (currentNode == 0 || currentNode->type() == Node::Type_Project) {
72         ui.ui_isAfter->setEnabled(false);
73         ui.ui_isParent->setEnabled(false);
74         ui.ui_useProject->setChecked(true);
75 
76         ui.ui_name->setText(project.name());
77     } else {
78         ui.ui_isAfter->setChecked(true);
79 
80         ui.ui_name->setText(currentNode->name());
81     }
82     connect(ui.ui_url, &KUrlRequester::textChanged, this, &InsertFilePanel::changed);
83 
84     connect(ui.ui_url, &KUrlRequester::openFileDialog, this, &InsertFilePanel::slotOpenFileDialog);
85 }
86 
slotOpenFileDialog(KUrlRequester *)87 void InsertFilePanel::slotOpenFileDialog(KUrlRequester *)
88 {
89     ui.ui_url->setFilter("*.plan");
90 }
91 
changed(const QString & text)92 void InsertFilePanel::changed(const QString &text)
93 {
94     KIO::StatJob* statJob = KIO::stat(QUrl::fromUserInput(text));
95     statJob->setSide(KIO::StatJob::SourceSide);
96 
97     const bool isUrlReadable = statJob->exec();
98 
99     emit enableButtonOk(isUrlReadable);
100 }
101 
url() const102 QUrl InsertFilePanel::url() const
103 {
104     return ui.ui_url->url();
105 }
106 
parentNode() const107 Node *InsertFilePanel::parentNode() const
108 {
109     if (ui.ui_useProject->isChecked()) {
110         return &(m_project);
111     }
112     if (ui.ui_isParent->isChecked()) {
113         return m_node;
114     }
115     if (ui.ui_isAfter->isChecked()) {
116         return m_node->parentNode();
117     }
118     return &(m_project);
119 }
120 
afterNode() const121 Node *InsertFilePanel::afterNode() const
122 {
123     if (ui.ui_isAfter->isChecked()) {
124         return m_node;
125     }
126     return 0;
127 }
128 
129 
130 }  //KPlato namespace
131