1 /*
2     *
3     * This file is a part of Libcprime.
4     * Library for saving activites and bookmarks, share file and more.
5 	* Copyright 2019 CuboCore Group
6     *
7 
8     *
9     * This program is free software; you can redistribute it and/or modify
10     * it under the terms of the GNU General Public License as published by
11     * the Free Software Foundation; either version 3 of the License, or
12     * (at your option) any later version.
13     *
14 
15     *
16     * This program is distributed in the hope that it will be useful,
17     * but WITHOUT ANY WARRANTY; without even the implied warranty of
18     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19     * GNU General Public License for more details.
20     *
21 
22     *
23     * You should have received a copy of the GNU General Public License
24     * along with this program; if not, vsit http://www.gnu.org/licenses/.
25     *
26 */
27 
28 
29 #include <QTimer>
30 #include <QFileInfo>
31 #include <QIcon>
32 #include <QInputDialog>
33 #include <QMessageBox>
34 
35 #include "filefunc.h"
36 #include "themefunc.h"
37 #include "messageengine.h"
38 
39 #include "pinit.h"
40 #include "ui_pinit.h"
41 
42 
PinIT(const QStringList & files,QWidget * parent)43 PinIT::PinIT(const QStringList &files, QWidget *parent) : QDialog( parent )
44   , ui(new Ui::PinIT)
45   , mFiles(files)
46 {
47     ui->setupUi(this);
48 
49     setWindowIcon(QIcon::fromTheme("bookmark-new"));
50 
51     // set the requried folders
52     CPrime::FileUtils::setupFolder(CPrime::FolderSetup::ConfigFolder);
53 
54 	connect(ui->cancel, &QToolButton::clicked, this, &PinIT::reject);
55 
56     ui->pinSection->clear();
57     ui->pinSection->addItems(pin.getPinSections());
58     ui->done->setEnabled(false);
59 
60     QFileInfo info(mFiles.at(0));
61     const QString str = pin.checkingPinPathEx(mFiles.at(0));
62 
63     if (str.count() == 0) {
64         QIcon ico = CPrime::ThemeFunc::getFileIcon(mFiles.at(0));
65         QPixmap pix = ico.pixmap(QSize(100, 80));
66         setPinPath(mFiles.at(0));
67         setPinName(info.fileName() + "");
68         checkPath();
69     } else {
70 		CPrime::MessageEngine::messageEngine("bookmark-new", "PinIT", "Can't pin selected file(s)", str);
71 		ui->pinName->setEnabled(false);
72 		ui->pinStatus->setText(str);
73 		ui->path->setText("");
74     }
75 }
76 
~PinIT()77 PinIT::~PinIT()
78 {
79     delete ui;
80 }
81 
on_done_clicked()82 void PinIT::on_done_clicked()
83 {
84     if (ui->pinName->text().count() == 0) {
85         ui->done->setEnabled(false);
86     }
87 
88     if (ui->pinName->text().count() != 0 && ui->pinSection->currentText().count() != 0) {
89         pin.addPin(getSectionName(), getPinName(), mFiles.at(0));
90         QTimer::singleShot(100, this, SLOT(close()));
91         // Function from utilities.cpp
92         qDebug()<< "Pin Added at '" + ui->pinSection->currentText();
93 		accept();
94     }
95 }
96 
pinName_Changed()97 void PinIT::pinName_Changed()
98 {
99     if (ui->pinName->text().count() > 0) {
100         QString str = pin.checkingPinName(ui->pinSection->currentText(), ui->pinName->text());
101 
102         if (str.count() > 0) {
103             ui->pinStatus->setText(str);
104             ui->done->setEnabled(false);
105         } else {
106             ui->pinStatus->setText(str);
107             ui->done->setEnabled(true);
108         }
109     } else {
110         ui->done->setEnabled(false);
111     }
112 }
113 
checkPath()114 void PinIT::checkPath()
115 {
116     QString str = pin.checkingPinPath(ui->pinSection->currentText(), ui->pinName->text());
117 
118     if (str.count() > 0) {
119         ui->pinStatus->setText(str);
120         ui->pinName->setEnabled(false);
121         ui->done->setEnabled(false);
122         ui->cancel->setText("OK");
123     } else {
124         ui->pinStatus->setText(str);
125         ui->pinName->setEnabled(true);
126         ui->done->setEnabled(true);
127         ui->cancel->setText("Cancel");
128     }
129 }
130 
setPinPath(const QString & path)131 void PinIT::setPinPath(const QString &path)
132 {
133     ui->path->setText(path);
134 }
135 
setPinName(const QString & bName)136 void PinIT::setPinName(const QString &bName)
137 {
138     ui->pinName->setText(bName);
139 }
140 
getPinName()141 QString PinIT::getPinName()
142 {
143     return ui->pinName->text();
144 }
145 
getSectionName()146 QString PinIT::getSectionName()
147 {
148     return ui->pinSection->currentText();
149 }
150 
item_Changed()151 void PinIT::item_Changed()
152 {
153     checkPath();
154     pinName_Changed();
155 }
156 
on_pinName_textChanged(const QString & arg1)157 void PinIT::on_pinName_textChanged(const QString &arg1)
158 {
159     Q_UNUSED(arg1)
160 
161     if (ui->pinName->text().count() > 0) {
162         QString str = pin.checkingPinName(ui->pinSection->currentText(), ui->pinName->text());
163 
164         if (str.count() > 0) {
165             ui->pinStatus->setText(str);
166             ui->done->setEnabled(false);
167         } else {
168             ui->pinStatus->setText(str);
169             ui->done->setEnabled(true);
170         }
171     } else {
172         ui->done->setEnabled(false);
173     }
174 }
175 
on_pinSection_currentIndexChanged(const QString & arg1)176 void PinIT::on_pinSection_currentIndexChanged(const QString &arg1)
177 {
178 	ui->deleteSection->setDisabled(arg1 == "Speed Dial");
179 
180     checkPath();
181     pinName_Changed();
182 }
183 
on_deleteSection_clicked()184 void PinIT::on_deleteSection_clicked()
185 {
186 	QString currentSection = ui->pinSection->currentText();
187 	if (currentSection == "Speed Dial")
188 		return;
189 
190 	int exec = QMessageBox::question(this, "Delete Section",
191 									 QString("Do you want to delete this section '%1'").arg(currentSection),
192 									 QMessageBox::No | QMessageBox::Yes);
193 
194 	if (exec == QMessageBox::Yes) {
195 		pin.delSection(currentSection);
196 		ui->pinSection->removeItem(ui->pinSection->currentIndex());
197 	}
198 }
199 
on_addSection_clicked()200 void PinIT::on_addSection_clicked()
201 {
202 	QString sectionName;
203 	bool sectionExists = false;
204 	bool ok;
205 	bool tryAgain = false;
206 	do {
207 		sectionName = QInputDialog::getText(this, "Create New Section", "Enter new section name: ", QLineEdit::Normal, "", &ok);
208 
209 		if (ok) {
210 			if (pin.getPinSections().contains(sectionName, Qt::CaseInsensitive)) {
211 				sectionExists = true;
212 				int exec = QMessageBox::question(this, "Section Exists",
213 												 "Given Section Exists.\nDo you want to enter another section?",
214 												 QMessageBox::Yes | QMessageBox::No);
215 				if (exec == QMessageBox::Yes)
216 					tryAgain = true;
217 				else
218 					tryAgain = false;
219 			} else {
220 				sectionExists = false;
221 				tryAgain = false;
222 			}
223 		}
224 
225 	} while (tryAgain);
226 
227 	if (ok && !sectionExists) {
228 		pin.addSection(sectionName);
229 		ui->pinSection->addItem(sectionName);
230 	}
231 }
232