1 /***************************************************************************
2  *   Copyright (C) 2005 by Joshua Keel <joshuakeel@gmail.com>              *
3  *             (C) 2007 by Jeremy Whiting <jpwhiting@kde.org>              *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
19  ***************************************************************************/
20 
21 #include "vocabsettings.h"
22 
23 #include <KConfigDialog>
24 #include <KNS3/DownloadDialog>
25 
26 #include <sharedkvtmlfiles.h>
27 
28 #include <QIcon>
29 #include <QFileInfo>
30 #include <QPointer>
31 
32 #include "kanagramsettings.h"
33 #include "vocabedit.h"
34 
VocabSettings(QWidget * parent)35 VocabSettings::VocabSettings(QWidget *parent)
36     : QWidget(parent)
37 {
38     setupUi(this);
39 
40     connect(lviewVocab, &QTreeWidget::currentItemChanged, this, &VocabSettings::slotSelectionChanged);
41 
42     btnDownloadNew->setIcon(QIcon::fromTheme(QStringLiteral("get-hot-new-stuff")));
43 
44     refreshView();
45 }
46 
~VocabSettings()47 VocabSettings::~VocabSettings()
48 {
49 }
50 
loadView()51 void VocabSettings::loadView()
52 {
53     lviewVocab->clear();
54 
55     SharedKvtmlFiles::sortDownloadedFiles();
56     QString language = KanagramSettings::dataLanguage();
57     m_fileList = SharedKvtmlFiles::fileNames(language);
58     m_titleList = SharedKvtmlFiles::titles(language);
59     m_commentList = SharedKvtmlFiles::comments(language);
60 
61     for (int i = 0; i < m_fileList.size(); i++)
62     {
63         QTreeWidgetItem *item = new QTreeWidgetItem(lviewVocab, 0);
64         item->setText( 0, m_titleList[i] );
65         item->setText( 1, m_commentList[i] );
66         m_itemMap[item] = i;
67     }
68 }
69 
refreshView()70 void VocabSettings::refreshView()
71 {
72     loadView();
73     emit widgetModified();
74 }
75 
on_btnEdit_clicked()76 void VocabSettings::on_btnEdit_clicked()
77 {
78     if (lviewVocab->currentItem())
79     {
80         int index = m_itemMap[lviewVocab->currentItem()];
81         VocabEdit *vocabEdit = new VocabEdit(this, m_fileList[index]);
82         connect(vocabEdit, &VocabEdit::finished, this, &VocabSettings::refreshView);
83         vocabEdit->show();
84     }
85 }
86 
on_btnCreateNew_clicked()87 void VocabSettings::on_btnCreateNew_clicked()
88 {
89     VocabEdit *vocabEdit = new VocabEdit(this, QLatin1String(""));
90     connect(vocabEdit, &VocabEdit::finished, this, &VocabSettings::refreshView);
91     vocabEdit->show();
92 }
93 
on_btnDownloadNew_clicked()94 void VocabSettings::on_btnDownloadNew_clicked()
95 {
96     QPointer<KNS3::DownloadDialog> dialog = new KNS3::DownloadDialog( QStringLiteral("kanagram.knsrc") );
97     dialog->exec();
98     if ( dialog->changedEntries().size() > 0 ){
99         refreshView();
100     }
101 
102     delete dialog;
103 }
104 
slotSelectionChanged(QTreeWidgetItem * item)105 void VocabSettings::slotSelectionChanged(QTreeWidgetItem *item)
106 {
107     int index = m_itemMap.value(item);
108     bool writeable = QFileInfo(m_fileList[index]).isWritable();
109     btnEdit->setEnabled(writeable);
110 }
111