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

lokalize

  • sources
  • kde-4.12
  • kdesdk
  • lokalize
  • src
  • tm
dbfilesmodel.cpp
Go to the documentation of this file.
1 /* ****************************************************************************
2  This file is part of Lokalize
3 
4  Copyright (C) 2007-2009 by Nick Shaforostoff <shafff@ukr.net>
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License as
8  published by the Free Software Foundation; either version 2 of
9  the License or (at your option) version 3 or any later version
10  accepted by the membership of KDE e.V. (or its successor approved
11  by the membership of KDE e.V.), which shall act as a proxy
12  defined in Section 14 of version 3 of the license.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program. If not, see <http://www.gnu.org/licenses/>.
21 
22 **************************************************************************** */
23 
24 #include "dbfilesmodel.h"
25 #include "jobs.h"
26 #include "project.h"
27 
28 #include <QCoreApplication>
29 #include <QFileSystemModel>
30 #include <QStringBuilder>
31 
32 #include <threadweaver/ThreadWeaver.h>
33 #include <kstandarddirs.h>
34 using namespace TM;
35 
36 static QString tmFileExtension(TM_DATABASE_EXTENSION);
37 static QString remoteTmExtension(REMOTETM_DATABASE_EXTENSION);
38 
39 
40 DBFilesModel* DBFilesModel::_instance=0;
41 void DBFilesModel::cleanupDBFilesModel()
42 {
43  delete DBFilesModel::_instance; DBFilesModel::_instance = 0;
44 }
45 
46 DBFilesModel* DBFilesModel::instance()
47 {
48  if (KDE_ISUNLIKELY( _instance==0 )) {
49  _instance=new DBFilesModel;
50  qAddPostRoutine(DBFilesModel::cleanupDBFilesModel);
51  }
52 
53  return _instance;
54 }
55 
56 
57 DBFilesModel::DBFilesModel()
58  : QSortFilterProxyModel()
59  , projectDB(0)
60  , m_fileSystemModel(new QFileSystemModel(this))
61  , m_tmRootPath(KStandardDirs::locateLocal("appdata", ""))
62 {
63  m_fileSystemModel->setNameFilters(QStringList(QString("*.") + TM_DATABASE_EXTENSION));
64  m_fileSystemModel->setFilter(QDir::Files);
65  m_fileSystemModel->setRootPath(KStandardDirs::locateLocal("appdata", ""));
66 
67  setSourceModel(m_fileSystemModel);
68  connect (this,SIGNAL(rowsInserted(QModelIndex,int,int)),
69  this,SLOT(calcStats(QModelIndex,int,int))/*,Qt::QueuedConnection*/);
70 
71  connect (this,SIGNAL(dataChanged(QModelIndex,QModelIndex)),
72  this,SLOT(updateStats(QModelIndex,QModelIndex)),Qt::QueuedConnection);
73  m_timeSinceLastUpdate.start();
74 
75  int count=rowCount(rootIndex());
76  if (count) calcStats(rootIndex(),0,count-1);
77  openDB("default"); //behave when no project is loaded
78 }
79 
80 DBFilesModel::~DBFilesModel()
81 {
82  delete projectDB;
83 }
84 
85 
86 bool DBFilesModel::filterAcceptsRow ( int source_row, const QModelIndex& source_parent ) const
87 {
88  if (source_parent!=m_fileSystemModel->index(m_tmRootPath))
89  return true;
90 
91  const QString& fileName=m_fileSystemModel->index(source_row, 0, source_parent).data().toString();
92  return (fileName.endsWith(tmFileExtension) && !fileName.endsWith("-journal.db")) || fileName.endsWith(remoteTmExtension);
93 }
94 
95 QModelIndex DBFilesModel::rootIndex() const
96 {
97  return mapFromSource(m_fileSystemModel->index(m_tmRootPath));
98 }
99 
100 QVariant DBFilesModel::headerData(int section, Qt::Orientation orientation, int role) const
101 {
102  Q_UNUSED(orientation);
103  if (role!=Qt::DisplayRole) return QVariant();
104 
105  const char* const columns[]={
106  I18N_NOOP2("@title:column","Name"),
107  I18N_NOOP2("@title:column","Source language"),
108  I18N_NOOP2("@title:column","Target language"),
109  I18N_NOOP2("@title:column","Pairs"),
110  I18N_NOOP2("@title:column","Unique original entries"),
111  I18N_NOOP2("@title:column","Unique translations")
112  };
113 
114  return i18nc("@title:column",columns[section]);
115 }
116 
117 void DBFilesModel::openDB(const QString& name)
118 {
119  if (QFileInfo(KStandardDirs::locateLocal("appdata", name % REMOTETM_DATABASE_EXTENSION)).exists())
120  openDB(name, TM::Remote);
121  else
122  openDB(name, TM::Local);
123 }
124 
125 void DBFilesModel::openDB(const QString& name, DbType type)
126 {
127  openDB(new OpenDBJob(name, type));
128 }
129 
130 void DBFilesModel::openDB(OpenDBJob* openDBJob)
131 {
132  connect(openDBJob,SIGNAL(done(ThreadWeaver::Job*)),openDBJob,SLOT(deleteLater()));
133  connect(openDBJob,SIGNAL(done(ThreadWeaver::Job*)),this,SLOT(openJobDone(ThreadWeaver::Job*)));
134  ThreadWeaver::Weaver::instance()->enqueue(openDBJob);
135 }
136 
137 void DBFilesModel::updateStats(const QModelIndex& topLeft, const QModelIndex& bottomRight)
138 {
139  if (m_timeSinceLastUpdate.elapsed()<60000
140  || !topLeft.isValid() || !bottomRight.isValid())
141  return;
142 
143  qDebug()<<"DBFilesModel::updateStats() called";
144  calcStats(topLeft.parent(), topLeft.row(), bottomRight.row());
145  m_timeSinceLastUpdate.start();
146 }
147 
148 void DBFilesModel::calcStats(const QModelIndex& parent, int start, int end)
149 {
150  if (parent!=rootIndex())
151  return;
152 
153  const QString& projectID=Project::instance()->projectID();
154  while (start<=end)
155  {
156  QModelIndex index=QSortFilterProxyModel::index(start++, 0, parent);
157  QString res=index.data().toString();
158  if (KDE_ISUNLIKELY(res==projectID && (!projectDB || data(*projectDB).toString()!=projectID)))
159  projectDB=new QPersistentModelIndex(index);//TODO if user switches the project
160 // if (KDE_ISLIKELY( QSqlDatabase::contains(res) ))
161 // continue;
162  openDB(res, DbType(index.data(NameRole).toString().endsWith(remoteTmExtension)));
163  }
164 }
165 
166 void DBFilesModel::openJobDone(ThreadWeaver::Job* job)
167 {
168  OpenDBJob* j=static_cast<OpenDBJob*>(job);
169  m_stats[j->m_dbName]=j->m_stat;
170  m_configurations[j->m_dbName]=j->m_tmConfig;
171  kDebug()<<j->m_dbName<<j->m_tmConfig.targetLangCode;
172 }
173 
174 void DBFilesModel::removeTM ( QModelIndex index )
175 {
176  index=index.sibling(index.row(),0);
177  CloseDBJob* closeDBJob=new CloseDBJob(index.data().toString());
178  connect(closeDBJob,SIGNAL(done(ThreadWeaver::Job*)),closeDBJob,SLOT(deleteLater()));
179  connect(closeDBJob,SIGNAL(done(ThreadWeaver::Job*)),this,SLOT(closeJobDone(ThreadWeaver::Job*)));
180  ThreadWeaver::Weaver::instance()->enqueue(closeDBJob);
181 }
182 
183 void DBFilesModel::closeJobDone(ThreadWeaver::Job* job)
184 {
185  CloseDBJob* j=static_cast<CloseDBJob*>(job);
186  QFile::remove(m_fileSystemModel->rootPath() + '/' + j->dbName() + tmFileExtension);
187 }
188 
189 void DBFilesModel::updateProjectTmIndex()
190 {
191  if (projectDB && data(*projectDB).toString()!=Project::instance()->projectID())
192  {
193  delete projectDB; projectDB=0;
194  }
195 }
196 
197 int DBFilesModel::columnCount (const QModelIndex&) const
198 {
199  return 4; //FIXME the lat two columns are not displayed even if 6 is returned
200 }
201 
202 
203 QVariant DBFilesModel::data (const QModelIndex& index, int role) const
204 {
205  if (role==Qt::DecorationRole) return QVariant();
206  if (role!=Qt::DisplayRole && role!=NameRole && index.column()<4) return QSortFilterProxyModel::data(index, role);
207  //if (role!=Qt::DisplayRole && role!=NameRole) return QVariant();
208 
209  QString res=QSortFilterProxyModel::data(index.sibling(index.row(), 0), QFileSystemModel::FileNameRole).toString();
210 
211  if (role==NameRole) return res;
212  if (res.endsWith(remoteTmExtension))
213  res.chop(remoteTmExtension.size());
214  else
215  res.chop(tmFileExtension.size());
216  //qDebug()<<m_stats[res].uniqueSourcesCount<<(index.column()==OriginalsCount);
217 
218  switch (index.column())
219  {
220  case Name: return res;
221  case SourceLang: return m_configurations[res].sourceLangCode;
222  case TargetLang: return m_configurations[res].targetLangCode;
223  case Pairs: return m_stats[res].pairsCount;
224  case OriginalsCount: return m_stats[res].uniqueSourcesCount;
225  case TranslationsCount: return m_stats[res].uniqueTranslationsCount;
226  }
227 
228  return res;
229 }
230 
TM::DbType
DbType
Definition: jobs.h:43
TM::DBFilesModel
Definition: dbfilesmodel.h:39
project.h
TM::DBFilesModel::NameRole
Definition: dbfilesmodel.h:57
TM::DBFilesModel::filterAcceptsRow
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
Definition: dbfilesmodel.cpp:86
remoteTmExtension
static QString remoteTmExtension(REMOTETM_DATABASE_EXTENSION)
TM::DBFilesModel::Name
Definition: dbfilesmodel.h:46
TM::OpenDBJob::m_stat
DBStat m_stat
Definition: jobs.h:98
TM::Local
Definition: jobs.h:43
TM::OpenDBJob::m_dbName
QString m_dbName
Definition: jobs.h:95
TM::DBFilesModel::m_configurations
QMap< QString, TMConfig > m_configurations
Definition: dbfilesmodel.h:102
TM::CloseDBJob::dbName
QString dbName()
Definition: jobs.h:118
Project::instance
static Project * instance()
Definition: project.cpp:67
TM::DBFilesModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Definition: dbfilesmodel.cpp:203
TM::OpenDBJob::m_tmConfig
TMConfig m_tmConfig
Definition: jobs.h:101
TM::Remote
Definition: jobs.h:43
dbfilesmodel.h
TM::DBFilesModel::SourceLang
Definition: dbfilesmodel.h:47
TM::DBFilesModel::updateStats
void updateStats(const QModelIndex &topLeft, const QModelIndex &bottomRight)
Definition: dbfilesmodel.cpp:137
TM::DBFilesModel::DBFilesModel
DBFilesModel()
Definition: dbfilesmodel.cpp:57
TM::DBFilesModel::calcStats
void calcStats(const QModelIndex &parent, int start, int end)
Definition: dbfilesmodel.cpp:148
TM::DBFilesModel::closeJobDone
void closeJobDone(ThreadWeaver::Job *)
Definition: dbfilesmodel.cpp:183
TM::DBFilesModel::TargetLang
Definition: dbfilesmodel.h:48
TM::DBFilesModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: dbfilesmodel.cpp:100
tmFileExtension
static QString tmFileExtension(TM_DATABASE_EXTENSION)
TM::OpenDBJob
Definition: jobs.h:74
TM::DBFilesModel::openJobDone
void openJobDone(ThreadWeaver::Job *)
Definition: dbfilesmodel.cpp:166
TM::TMConfig::targetLangCode
QString targetLangCode
Definition: jobs.h:68
TM::DBFilesModel::OriginalsCount
Definition: dbfilesmodel.h:50
TM::DBFilesModel::openDB
void openDB(const QString &name)
Definition: dbfilesmodel.cpp:117
TM::DBFilesModel::TranslationsCount
Definition: dbfilesmodel.h:51
QSortFilterProxyModel
TM_DATABASE_EXTENSION
#define TM_DATABASE_EXTENSION
Definition: jobs.h:41
TM::DBFilesModel::~DBFilesModel
~DBFilesModel()
Definition: dbfilesmodel.cpp:80
jobs.h
REMOTETM_DATABASE_EXTENSION
#define REMOTETM_DATABASE_EXTENSION
Definition: jobs.h:42
ProjectBase::projectID
QString projectID() const
Get ProjectID.
Definition: projectbase.h:31
TM::DBFilesModel::rootIndex
QModelIndex rootIndex() const
Definition: dbfilesmodel.cpp:95
TM::CloseDBJob
Definition: jobs.h:110
TM::DBFilesModel::Pairs
Definition: dbfilesmodel.h:49
TM::DBFilesModel::columnCount
int columnCount(const QModelIndex &parent=QModelIndex()) const
Definition: dbfilesmodel.cpp:197
TM::DBFilesModel::updateProjectTmIndex
void updateProjectTmIndex()
Definition: dbfilesmodel.cpp:189
TM::DBFilesModel::removeTM
void removeTM(QModelIndex)
Definition: dbfilesmodel.cpp:174
TM::DBFilesModel::instance
static DBFilesModel * instance()
Definition: dbfilesmodel.cpp:46
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:03:45 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
  • okteta
  • 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