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