QtPass  1.2.3
Multi-platform GUI for pass, the standard unix password manager.
storemodel.cpp
Go to the documentation of this file.
1 #include "storemodel.h"
2 #include "qtpasssettings.h"
3 
4 #include <QDebug>
5 #include <QMessageBox>
6 #include <QMimeData>
7 
8 QDataStream &
9 operator<<(QDataStream &out,
14  return out;
15 }
16 
17 QDataStream &
18 operator>>(QDataStream &in,
22  return in;
23 }
24 
30 StoreModel::StoreModel() { fs = NULL; }
31 
39 bool StoreModel::filterAcceptsRow(int sourceRow,
40  const QModelIndex &sourceParent) const {
41  QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
42  return ShowThis(index);
43 }
44 
51 bool StoreModel::ShowThis(const QModelIndex index) const {
52  bool retVal = false;
53  if (fs == NULL)
54  return retVal;
55  // Gives you the info for number of childs with a parent
56  if (sourceModel()->rowCount(index) > 0) {
57  for (int nChild = 0; nChild < sourceModel()->rowCount(index); ++nChild) {
58  QModelIndex childIndex = sourceModel()->index(nChild, 0, index);
59  if (!childIndex.isValid())
60  break;
61  retVal = ShowThis(childIndex);
62  if (retVal)
63  break;
64  }
65  } else {
66  QModelIndex useIndex = sourceModel()->index(index.row(), 0, index.parent());
67  QString path = fs->filePath(useIndex);
68  path = QDir(store).relativeFilePath(path);
69  path.replace(QRegExp("\\.gpg$"), "");
70  retVal = path.contains(filterRegExp());
71  }
72  return retVal;
73 }
74 
80 void StoreModel::setModelAndStore(QFileSystemModel *sourceModel,
81  QString passStore) {
82  fs = sourceModel;
83  store = passStore;
84 }
85 
92 QVariant StoreModel::data(const QModelIndex &index, int role) const {
93  if (!index.isValid())
94  return QVariant();
95 
96  QVariant initial_value;
97  initial_value = QSortFilterProxyModel::data(index, role);
98 
99  if (role == Qt::DisplayRole) {
100  QString name = initial_value.toString();
101  name.replace(QRegExp("\\.gpg$"), "");
102  initial_value.setValue(name);
103  }
104 
105  return initial_value;
106 }
107 
112 Qt::DropActions StoreModel::supportedDropActions() const {
113  return Qt::CopyAction | Qt::MoveAction;
114 }
119 Qt::DropActions StoreModel::supportedDragActions() const {
120  return Qt::CopyAction | Qt::MoveAction;
121 }
122 
123 Qt::ItemFlags StoreModel::flags(const QModelIndex &index) const {
124  Qt::ItemFlags defaultFlags = QSortFilterProxyModel::flags(index);
125 
126  if (index.isValid()) {
127  return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
128  } else {
129  return Qt::ItemIsDropEnabled | defaultFlags;
130  }
131 }
132 
133 QStringList StoreModel::mimeTypes() const {
134  QStringList types;
135  types << "application/vnd+qtpass.dragAndDropInfoPasswordStore";
136  return types;
137 }
138 
139 QMimeData *StoreModel::mimeData(const QModelIndexList &indexes) const {
141 
142  QByteArray encodedData;
143  // only use the first, otherwise we should enable multiselection
144  QModelIndex index = indexes.at(0);
145  if (index.isValid()) {
146  QModelIndex useIndex = mapToSource(index);
147 
148  info.isDir = fs->fileInfo(useIndex).isDir();
149  info.isFile = fs->fileInfo(useIndex).isFile();
150  info.path = fs->fileInfo(useIndex).absoluteFilePath();
151  QDataStream stream(&encodedData, QIODevice::WriteOnly);
152  stream << info;
153  }
154 
155  QMimeData *mimeData = new QMimeData();
156  mimeData->setData("application/vnd+qtpass.dragAndDropInfoPasswordStore",
157  encodedData);
158  return mimeData;
159 }
160 
161 bool StoreModel::canDropMimeData(const QMimeData *data, Qt::DropAction action,
162  int row, int column,
163  const QModelIndex &parent) const {
164 #ifdef QT_DEBUG
165  qDebug() << action << row;
166 #else
167  Q_UNUSED(action)
168  Q_UNUSED(row)
169 #endif
170 
171  QModelIndex useIndex =
172  this->index(parent.row(), parent.column(), parent.parent());
173  QByteArray encodedData =
174  data->data("application/vnd+qtpass.dragAndDropInfoPasswordStore");
175  QDataStream stream(&encodedData, QIODevice::ReadOnly);
177  stream >> info;
178  if (data->hasFormat("application/vnd+qtpass.dragAndDropInfoPasswordStore") ==
179  false)
180  return false;
181 
182  if (column > 0) {
183  return false;
184  }
185 
186  // you can drop a folder on a folder
187  if (fs->fileInfo(mapToSource(useIndex)).isDir() && info.isDir) {
188  return true;
189  }
190  // you can drop a file on a folder
191  if (fs->fileInfo(mapToSource(useIndex)).isDir() && info.isFile) {
192  return true;
193  }
194  // you can drop a file on a file
195  if (fs->fileInfo(mapToSource(useIndex)).isFile() && info.isFile) {
196  return true;
197  }
198 
199  return false;
200 }
201 
202 bool StoreModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
203  int row, int column, const QModelIndex &parent) {
204  if (!canDropMimeData(data, action, row, column, parent))
205  return false;
206 
207  if (action == Qt::IgnoreAction) {
208  return true;
209  }
210  QByteArray encodedData =
211  data->data("application/vnd+qtpass.dragAndDropInfoPasswordStore");
212 
213  QDataStream stream(&encodedData, QIODevice::ReadOnly);
215  stream >> info;
216  QModelIndex destIndex =
217  this->index(parent.row(), parent.column(), parent.parent());
218  QFileInfo destFileinfo = fs->fileInfo(mapToSource(destIndex));
219  QFileInfo srcFileInfo = QFileInfo(info.path);
220  QDir qdir;
221  QString cleanedSrc = qdir.cleanPath(srcFileInfo.absoluteFilePath());
222  QString cleanedDest = qdir.cleanPath(destFileinfo.absoluteFilePath());
223  if (info.isDir) {
224  QDir srcDir = QDir(info.path);
225  // dropped dir onto dir
226  if (destFileinfo.isDir()) {
227  QDir destDir = QDir(cleanedDest).filePath(srcFileInfo.fileName());
228  QString cleanedDestDir = qdir.cleanPath(destDir.absolutePath());
229  if (action == Qt::MoveAction) {
230  QtPassSettings::getPass()->Move(cleanedSrc, cleanedDestDir);
231  } else if (action == Qt::CopyAction) {
232  QtPassSettings::getPass()->Copy(cleanedSrc, cleanedDestDir);
233  }
234  }
235  } else if (info.isFile) {
236  // dropped file onto a directory
237  if (destFileinfo.isDir()) {
238  if (action == Qt::MoveAction) {
239  QtPassSettings::getPass()->Move(cleanedSrc, cleanedDest);
240  } else if (action == Qt::CopyAction) {
241  QtPassSettings::getPass()->Copy(cleanedSrc, cleanedDest);
242  }
243  } else if (destFileinfo.isFile()) {
244  // dropped file onto a file
245  int answer = QMessageBox::question(
246  0, tr("force overwrite?"),
247  tr("overwrite %1 with %2?").arg(cleanedDest).arg(cleanedSrc),
248  QMessageBox::Yes | QMessageBox::No);
249  bool force = answer == QMessageBox::Yes;
250  if (action == Qt::MoveAction) {
251  QtPassSettings::getPass()->Move(cleanedSrc, cleanedDest, force);
252  } else if (action == Qt::CopyAction) {
253  QtPassSettings::getPass()->Copy(cleanedSrc, cleanedDest, force);
254  }
255  }
256  }
257  return true;
258 }
virtual void Copy(const QString srcDir, const QString dest, const bool force=false)=0
bool ShowThis(const QModelIndex) const
StoreModel::ShowThis should a row be shown, based on our search criteria.
Definition: storemodel.cpp:51
Qt::DropActions supportedDragActions() const
StoreModel::supportedDragActions enable drag.
Definition: storemodel.cpp:119
virtual void Move(const QString srcDir, const QString dest, const bool force=false)=0
QMimeData * mimeData(const QModelIndexList &indexes) const
Definition: storemodel.cpp:139
bool filterAcceptsRow(int, const QModelIndex &) const
StoreModel::filterAcceptsRow should row be shown, wrapper for StoreModel::ShowThis method...
Definition: storemodel.cpp:39
Qt::DropActions supportedDropActions() const
StoreModel::supportedDropActions enable drop.
Definition: storemodel.cpp:112
def data
Definition: avatars.py:87
bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const
Definition: storemodel.cpp:161
void setModelAndStore(QFileSystemModel *sourceModel, QString passStore)
StoreModel::setModelAndStore update the source model and store.
Definition: storemodel.cpp:80
Qt::ItemFlags flags(const QModelIndex &index) const
Definition: storemodel.cpp:123
QVariant data(const QModelIndex &index, int role) const
StoreModel::data don&#39;t show the .gpg at the end of a file.
Definition: storemodel.cpp:92
static Pass * getPass()
QStringList mimeTypes() const
Definition: storemodel.cpp:133
QDataStream & operator<<(QDataStream &out, const dragAndDropInfoPasswordStore &dragAndDropInfoPasswordStore)
Definition: storemodel.cpp:9
StoreModel()
StoreModel::StoreModel SubClass of QSortFilterProxyModel via http://www.qtcentre.org/threads/46471-QT...
Definition: storemodel.cpp:30
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
Definition: storemodel.cpp:202
QDataStream & operator>>(QDataStream &in, dragAndDropInfoPasswordStore &dragAndDropInfoPasswordStore)
Definition: storemodel.cpp:18