• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

lokalize

  • sources
  • kde-4.14
  • kdesdk
  • lokalize
  • src
  • tm
tmmanager.cpp
Go to the documentation of this file.
1 /* ****************************************************************************
2  This file is part of Lokalize
3 
4  Copyright (C) 2007-2011 by Nick Shaforostoff <shafff@ukr.net>
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 2 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, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20  In addition, as a special exception, the copyright holders give
21  permission to link the code of this program with any edition of
22  the Qt library by Trolltech AS, Norway (or with modified versions
23  of Qt that use the same license as Qt), and distribute linked
24  combinations including the two. You must obey the GNU General
25  Public License in all respects for all of the code used other than
26  Qt. If you modify this file, you may extend this exception to
27  your version of the file, but you are not obligated to do so. If
28  you do not wish to do so, delete this exception statement from
29  your version.
30 
31 **************************************************************************** */
32 
33 #include "tmmanager.h"
34 #include "ui_managedatabases.h"
35 #include "dbfilesmodel.h"
36 #include "tmtab.h"
37 #include "jobs.h"
38 #include "tmscanapi.h"
39 #include "project.h"
40 #include "languagelistmodel.h"
41 
42 #include <QTimer>
43 #include <QSortFilterProxyModel>
44 #include <QStringBuilder>
45 #include <kfiledialog.h>
46 #include <kdebug.h>
47 // #include <kstandarddirs.h>
48 #include <threadweaver/ThreadWeaver.h>
49 #include <kstandarddirs.h>
50 
51 using namespace TM;
52 
53 TMManagerWin::TMManagerWin(QWidget *parent)
54  : KMainWindow(parent)
55 {
56  setAttribute(Qt::WA_DeleteOnClose, false);
57  setCaption(i18nc("@title:window", "Translation Memories"));
58  setCentralWidget(new QWidget(this));
59  Ui_TMManager ui_tmManager;
60  ui_tmManager.setupUi(centralWidget());
61 
62  ui_tmManager.list->setModel(DBFilesModel::instance());
63  ui_tmManager.list->setRootIndex(DBFilesModel::instance()->rootIndex());
64  m_tmListWidget=ui_tmManager.list;
65 
66  connect(ui_tmManager.addData, SIGNAL(clicked(bool)),this,SLOT(addDir()));
67  connect(ui_tmManager.create, SIGNAL(clicked(bool)),this,SLOT(addDB()));
68  connect(ui_tmManager.importTMX,SIGNAL(clicked(bool)),this,SLOT(importTMX()));
69  connect(ui_tmManager.exportTMX,SIGNAL(clicked(bool)),this,SLOT(exportTMX()));
70  connect(ui_tmManager.remove, SIGNAL(clicked(bool)),this,SLOT(removeDB()));
71 
72  QTimer::singleShot(100,this,SLOT(initLater()));
73 }
74 
75 
76 void TMManagerWin::initLater()
77 {
78  connect(m_tmListWidget,SIGNAL(activated(QModelIndex)),this,SLOT(slotItemActivated(QModelIndex)));
79 
80  QPersistentModelIndex* projectDBIndex=DBFilesModel::instance()->projectDBIndex();
81  if (projectDBIndex)
82  m_tmListWidget->setCurrentIndex(*projectDBIndex);
83 }
84 
85 void TMManagerWin::addDir()
86 {
87  QModelIndex index=m_tmListWidget->currentIndex();
88  if (!index.isValid())
89  return;
90 
91  QString dir=KFileDialog::getExistingDirectory(KUrl("kfiledialog:///tm-food"),this,
92  i18nc("@title:window","Select Directory to be scanned"));
93  if (!dir.isEmpty())
94  {
95  QList<QUrl> dirs; dirs.append(QUrl(dir));
96  scanRecursive(dirs,index.sibling(index.row(), 0).data().toString());
97  }
98 }
99 
100 
101 DBPropertiesDialog::DBPropertiesDialog(QWidget* parent, const QString& dbName)
102  : KDialog(parent), Ui_DBParams()
103  , m_connectionOptionsValid(false)
104 {
105  setAttribute(Qt::WA_DeleteOnClose, true);
106  setCaption( dbName.isEmpty()?i18nc("@title:window","New Translation Memory"):i18nc("@title:window","Translation Memory Properties"));
107  setButtons( KDialog::Ok | KDialog::Cancel);
108  enableButtonOk(false);
109 
110  setupUi(mainWidget());
111  name->setFocus();
112  connect(name, SIGNAL(textChanged(QString)), this, SLOT(feedbackRegardingAcceptable()));
113 
114  sourceLang->setModel(LanguageListModel::instance()->sortModel());
115  targetLang->setModel(LanguageListModel::instance()->sortModel());
116 
117  if (dbName.isEmpty())
118  {
119  accel->setText(Project::instance()->accel());
120  markup->setText(Project::instance()->markup());
121  sourceLang->setCurrentIndex(LanguageListModel::instance()->sortModelRowForLangCode( Project::instance()->sourceLangCode() ));
122  targetLang->setCurrentIndex(LanguageListModel::instance()->sortModelRowForLangCode( Project::instance()->targetLangCode() ));
123  }
124 
125  connectionBox->hide();
126  connect(dbType, SIGNAL(activated(int)), this, SLOT(setConnectionBoxVisible(int)));
127  m_checkDelayer.setInterval(2000);
128  m_checkDelayer.setSingleShot(true);
129  connect(&m_checkDelayer, SIGNAL(timeout()), this, SLOT(checkConnectionOptions()));
130  connect(this->dbName, SIGNAL(textChanged(QString)), &m_checkDelayer, SLOT(start()));
131  connect(dbHost->lineEdit(), SIGNAL(textChanged(QString)), &m_checkDelayer, SLOT(start()));
132  connect(dbUser, SIGNAL(textChanged(QString)), &m_checkDelayer, SLOT(start()));
133  connect(dbPasswd, SIGNAL(textChanged(QString)), &m_checkDelayer, SLOT(start()));
134 
135  QStringList drivers=QSqlDatabase::drivers();
136  if (drivers.contains("QPSQL"))
137  dbType->addItem("PostgreSQL");
138 }
139 
140 void DBPropertiesDialog::setConnectionBoxVisible(int type)
141 {
142  connectionBox->setVisible(type);
143  contentBox->setVisible(!type || m_connectionOptionsValid);
144 }
145 
146 void DBPropertiesDialog::feedbackRegardingAcceptable()
147 {
148  enableButtonOk(contentBox->isVisible() && !name->text().isEmpty());
149 }
150 
151 void DBPropertiesDialog::checkConnectionOptions()
152 {
153  m_connectionOptionsValid=false;
154  if (!connectionBox->isVisible() || name->text().isEmpty() || dbHost->currentText().isEmpty() || dbName->text().isEmpty() || dbUser->text().isEmpty())
155  return;
156 
157  OpenDBJob::ConnectionParams connParams;
158  connParams.driver="QPSQL";
159  connParams.host=dbHost->currentText();
160  connParams.db=dbName->text();
161  connParams.user=dbUser->text();
162  connParams.passwd=dbPasswd->text();
163 
164  OpenDBJob* openDBJob=new OpenDBJob(name->text(), TM::Remote, /*reconnect*/true, connParams);
165  connect(openDBJob,SIGNAL(done(ThreadWeaver::Job*)),openDBJob,SLOT(deleteLater()));
166  connect(openDBJob,SIGNAL(done(ThreadWeaver::Job*)),this,SLOT(openJobDone(ThreadWeaver::Job*)));
167  ThreadWeaver::Weaver::instance()->enqueue(openDBJob);
168 }
169 
170 void DBPropertiesDialog::openJobDone(ThreadWeaver::Job* job)
171 {
172  if (!connectionBox->isVisible()) //smth happened while we were trying to connect
173  return;
174 
175  OpenDBJob* openDBJob=static_cast<OpenDBJob*>(job);
176  contentBox->setVisible(openDBJob->m_connectionSuccessful);
177  enableButtonOk(openDBJob->m_connectionSuccessful);
178  if (!openDBJob->m_connectionSuccessful)
179  return;
180 
181  sourceLang->setCurrentIndex(LanguageListModel::instance()->sortModelRowForLangCode(openDBJob->m_tmConfig.sourceLangCode));
182  targetLang->setCurrentIndex(LanguageListModel::instance()->sortModelRowForLangCode(openDBJob->m_tmConfig.targetLangCode));
183  markup->setText(openDBJob->m_tmConfig.markup);
184  accel->setText(openDBJob->m_tmConfig.accel);
185  contentBox->show();
186 
187  dbHost->lineEdit()->setText(openDBJob->m_connParams.host);
188  dbName->setText(openDBJob->m_connParams.db);
189  dbUser->setText(openDBJob->m_connParams.user);
190  dbPasswd->setText(openDBJob->m_connParams.passwd);
191 }
192 
193 void DBPropertiesDialog::accept()
194 {
195  if (name->text().isEmpty() || !contentBox->isVisible())
196  return;
197 
198  if (connectionBox->isVisible())
199  {
200  QFile rdb(KStandardDirs::locateLocal("appdata", name->text() % REMOTETM_DATABASE_EXTENSION));
201  if (!rdb.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate))
202  return;
203 
204  QTextStream rdbParams(&rdb);
205  rdbParams<<"QPSQL"<<"\n";
206  rdbParams<<dbHost->currentText()<<"\n";
207  rdbParams<<dbName->text()<<"\n";
208  rdbParams<<dbUser->text()<<"\n";
209  rdbParams<<dbPasswd->text()<<"\n";
210  }
211 
212  OpenDBJob* openDBJob=new OpenDBJob(name->text(), TM::DbType(connectionBox->isVisible()), true);
213  connect(openDBJob,SIGNAL(done(ThreadWeaver::Job*)),DBFilesModel::instance(),SLOT(updateProjectTmIndex()));
214 
215  openDBJob->m_setParams=true;
216  openDBJob->m_tmConfig.markup=markup->text();
217  openDBJob->m_tmConfig.accel=accel->text();
218  openDBJob->m_tmConfig.sourceLangCode=LanguageListModel::instance()->langCodeForSortModelRow(sourceLang->currentIndex());
219  openDBJob->m_tmConfig.targetLangCode=LanguageListModel::instance()->langCodeForSortModelRow(targetLang->currentIndex());
220 
221  DBFilesModel::instance()->openDB(openDBJob);
222  KDialog::accept();
223 }
224 
225 void TMManagerWin::addDB()
226 {
227  DBPropertiesDialog* dialog=new DBPropertiesDialog(this);
228  dialog->show();
229 }
230 
231 void TMManagerWin::removeDB()
232 {
233  QModelIndex index=m_tmListWidget->currentIndex();
234  if (index.isValid())
235  DBFilesModel::instance()->removeTM(index);
236 }
237 
238 
239 void TMManagerWin::importTMX()
240 {
241  QString path=KFileDialog::getOpenFileName(KUrl("kfiledialog:///tmx"),
242  i18n("*.tmx *.xml|TMX files\n*|All files"),
243  this,
244  i18nc("@title:window","Select TMX file to be imported into selected database"));
245 
246  QModelIndex index=m_tmListWidget->currentIndex();
247  if (!index.isValid())
248  return;
249  QString dbName=index.sibling(index.row(), 0).data().toString();
250 
251  if (!path.isEmpty())
252  {
253  ImportTmxJob* j=new ImportTmxJob(path,dbName);
254  connect(j,SIGNAL(failed(ThreadWeaver::Job*)),j,SLOT(deleteLater()));
255  connect(j,SIGNAL(done(ThreadWeaver::Job*)),j,SLOT(deleteLater()));
256 
257  ThreadWeaver::Weaver::instance()->enqueue(j);
258  DBFilesModel::instance()->openDB(dbName); //update stats after it finishes
259  }
260 }
261 
262 
263 void TMManagerWin::exportTMX()
264 {
265  //TODO ask whether to save full paths of files, or just their names
266  QString path=KFileDialog::getSaveFileName(KUrl("kfiledialog:///tmx"),
267  i18n("*.tmx *.xml|TMX files\n*|All files"),
268  this,
269  i18nc("@title:window","Select TMX file to export selected database to"));
270 
271  QModelIndex index=m_tmListWidget->currentIndex();
272  if (!index.isValid())
273  return;
274  QString dbName=index.sibling(index.row(), 0).data().toString();
275 
276  if (!path.isEmpty())
277  {
278  ExportTmxJob* j=new ExportTmxJob(path,dbName);
279  connect(j,SIGNAL(done(ThreadWeaver::Job*)),j,SLOT(deleteLater()));
280  ThreadWeaver::Weaver::instance()->enqueue(j);
281  }
282 }
283 
284 void TMManagerWin::slotItemActivated(const QModelIndex&)
285 {
286  //QString dbName=DBFilesModel::instance()->data(m_tmListWidget->currentIndex()).toString();
287 /* TMWindow* win=new TMWindow;
288  win->selectDB(m_tmListWidget->currentIndex().row());
289  win->show();*/
290 }
291 
292 
293 
294 #include "tmmanager.moc"
TM::DbType
DbType
Definition: jobs.h:43
QTimer::setInterval
void setInterval(int msec)
LanguageListModel::instance
static LanguageListModel * instance()
Definition: languagelistmodel.cpp:46
QModelIndex
TM::TMConfig::accel
QString accel
Definition: jobs.h:66
TM::scanRecursive
int scanRecursive(const QList< QUrl > &urls, const QString &dbName)
wrapper. returns gross number of jobs started
Definition: tmscanapi.cpp:92
project.h
QWidget
TM::ExportTmxJob
Definition: jobs.h:329
QAbstractItemView::setCurrentIndex
void setCurrentIndex(const QModelIndex &index)
QSqlDatabase::drivers
QStringList drivers()
TM::OpenDBJob::ConnectionParams::user
QString user
Definition: jobs.h:80
tmtab.h
Project::instance
static Project * instance()
Definition: project.cpp:67
QStringList::contains
bool contains(const QString &str, Qt::CaseSensitivity cs) const
TM::OpenDBJob::m_connectionSuccessful
bool m_connectionSuccessful
Definition: jobs.h:104
TM::OpenDBJob::m_tmConfig
TMConfig m_tmConfig
Definition: jobs.h:101
languagelistmodel.h
LanguageListModel::langCodeForSortModelRow
QString langCodeForSortModelRow(int)
Definition: languagelistmodel.cpp:124
Ui_DBParams
TM::Remote
Definition: jobs.h:43
dbfilesmodel.h
QFile
QTextStream
TM::OpenDBJob::ConnectionParams::passwd
QString passwd
Definition: jobs.h:80
TM::OpenDBJob::ConnectionParams::db
QString db
Definition: jobs.h:80
QModelIndex::isValid
bool isValid() const
tmmanager.h
QList::append
void append(const T &value)
KMainWindow
QString::isEmpty
bool isEmpty() const
QModelIndex::row
int row() const
TM::DBPropertiesDialog::DBPropertiesDialog
DBPropertiesDialog(QWidget *parent, const QString &name=QString())
Definition: tmmanager.cpp:101
TM::OpenDBJob
Definition: jobs.h:74
TM::DBFilesModel::projectDBIndex
QPersistentModelIndex * projectDBIndex() const
Definition: dbfilesmodel.h:73
QString
QList
QStringList
TM::OpenDBJob::ConnectionParams::driver
QString driver
Definition: jobs.h:80
TM::OpenDBJob::m_setParams
bool m_setParams
Definition: jobs.h:102
TM::OpenDBJob::m_connParams
ConnectionParams m_connParams
Definition: jobs.h:106
TM::TMConfig::targetLangCode
QString targetLangCode
Definition: jobs.h:68
TM::TMManagerWin::TMManagerWin
TMManagerWin(QWidget *parent=0)
Definition: tmmanager.cpp:53
TM::DBFilesModel::openDB
void openDB(const QString &name)
Definition: dbfilesmodel.cpp:117
TM::TMConfig::sourceLangCode
QString sourceLangCode
Definition: jobs.h:67
QUrl
TM::OpenDBJob::ConnectionParams
Definition: jobs.h:78
jobs.h
QPersistentModelIndex
REMOTETM_DATABASE_EXTENSION
#define REMOTETM_DATABASE_EXTENSION
Definition: jobs.h:42
TM::TMConfig::markup
QString markup
Definition: jobs.h:65
TM::DBPropertiesDialog
Definition: tmmanager.h:70
QModelIndex::sibling
QModelIndex sibling(int row, int column) const
tmscanapi.h
TM::OpenDBJob::ConnectionParams::host
QString host
Definition: jobs.h:80
QAbstractItemView::currentIndex
QModelIndex currentIndex() const
TM::DBFilesModel::removeTM
void removeTM(QModelIndex)
Definition: dbfilesmodel.cpp:174
TM::ImportTmxJob
Definition: jobs.h:303
TM::DBFilesModel::instance
static DBFilesModel * instance()
Definition: dbfilesmodel.cpp:46
QTimer::singleShot
singleShot
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:40:07 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

lokalize

Skip menu "lokalize"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal