1 /* Copyright (C) 2006 J.F.Dockes
2  *   This program is free software; you can redistribute it and/or modify
3  *   it under the terms of the GNU General Public License as published by
4  *   the Free Software Foundation; either version 2 of the License, or
5  *   (at your option) any later version.
6  *
7  *   This program is distributed in the hope that it will be useful,
8  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *   GNU General Public License for more details.
11  *
12  *   You should have received a copy of the GNU General Public License
13  *   along with this program; if not, write to the
14  *   Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16  */
17 #include "autoconfig.h"
18 
19 #include <stdio.h>
20 
21 #include <vector>
22 #include <utility>
23 #include <string>
24 
25 using namespace std;
26 
27 #include <qpushbutton.h>
28 #include <qtimer.h>
29 
30 #include <qlistwidget.h>
31 
32 #include <qmessagebox.h>
33 #include <qinputdialog.h>
34 #include <qlayout.h>
35 
36 #include "recoll.h"
37 #include "log.h"
38 #include "guiutils.h"
39 #include "conftree.h"
40 
41 #include "ptrans_w.h"
42 
init(const string & dbdir)43 void EditTrans::init(const string& dbdir)
44 {
45     m_dbdir = path_canon(dbdir);
46     connect(transTW, SIGNAL(itemDoubleClicked(QTableWidgetItem *)),
47         this, SLOT(onItemDoubleClicked(QTableWidgetItem *)));
48     connect(cancelPB, SIGNAL(clicked()), this, SLOT(close()));
49 
50     QString lab = whatIdxLA->text();
51     lab.append(path2qs(m_dbdir));
52     whatIdxLA->setText(lab);
53 
54     QStringList labels(tr("Source path"));
55     labels.push_back(tr("Local path"));
56     transTW->setHorizontalHeaderLabels(labels);
57 
58     ConfSimple *conftrans = theconfig->getPTrans();
59     if (!conftrans)
60     return;
61 
62     int row = 0;
63     vector<string> opaths = conftrans->getNames(m_dbdir);
64     for (vector<string>::const_iterator it = opaths.begin();
65      it != opaths.end(); it++) {
66     transTW->setRowCount(row+1);
67     transTW->setItem(row, 0, new QTableWidgetItem(path2qs(*it)));
68     string npath;
69     conftrans->get(*it, npath, m_dbdir);
70     transTW->setItem(row, 1, new QTableWidgetItem(path2qs(npath)));
71     row++;
72     }
73 
74     resize(QSize(640, 300).expandedTo(minimumSizeHint()));
75 }
76 
onItemDoubleClicked(QTableWidgetItem * item)77 void EditTrans::onItemDoubleClicked(QTableWidgetItem *item)
78 {
79     transTW->editItem(item);
80 }
81 
on_savePB_clicked()82 void EditTrans::on_savePB_clicked()
83 {
84     ConfSimple *conftrans = theconfig->getPTrans();
85     if (!conftrans) {
86     QMessageBox::warning(0, "Recoll", tr("Config error"));
87     return;
88     }
89     conftrans->holdWrites(true);
90     conftrans->eraseKey(m_dbdir);
91 
92     for (int row = 0; row < transTW->rowCount(); row++) {
93     QTableWidgetItem *item0 = transTW->item(row, 0);
94     string from = path_canon(qs2path(item0->text()));
95     QTableWidgetItem *item1 = transTW->item(row, 1);
96     string to = path_canon(qs2path(item1->text()));
97     conftrans->set(from, to, m_dbdir);
98     }
99     conftrans->holdWrites(false);
100     // The rcldb does not use the same configuration object, but a
101     // copy. Force a reopen, this is quick.
102     string reason;
103     maybeOpenDb(reason, true);
104     close();
105 }
106 
on_addPB_clicked()107 void EditTrans::on_addPB_clicked()
108 {
109     transTW->setRowCount(transTW->rowCount()+1);
110     int row = transTW->rowCount()-1;
111     transTW->setItem(row, 0, new QTableWidgetItem(tr("Original path")));
112     transTW->setItem(row, 1, new QTableWidgetItem(tr("Local path")));
113     transTW->editItem(transTW->item(row, 0));
114 }
115 
on_delPB_clicked()116 void EditTrans::on_delPB_clicked()
117 {
118     QModelIndexList indexes = transTW->selectionModel()->selectedIndexes();
119     vector<int> rows;
120     for (int i = 0; i < indexes.size(); i++) {
121     rows.push_back(indexes.at(i).row());
122     }
123     sort(rows.begin(), rows.end());
124     rows.resize(unique(rows.begin(), rows.end()) - rows.begin());
125     for (int i = rows.size()-1; i >= 0; i--) {
126     transTW->removeRow(rows[i]);
127     }
128 }
129 
on_transTW_itemSelectionChanged()130 void EditTrans::on_transTW_itemSelectionChanged()
131 {
132     QModelIndexList indexes = transTW->selectionModel()->selectedIndexes();
133     if(indexes.size() < 1)
134     delPB->setEnabled(0);
135     else
136     delPB->setEnabled(1);
137 }
138 
139