QtPass  1.2.1
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 #endif
167 
168  QModelIndex useIndex =
169  this->index(parent.row(), parent.column(), parent.parent());
170  QByteArray encodedData =
171  data->data("application/vnd+qtpass.dragAndDropInfoPasswordStore");
172  QDataStream stream(&encodedData, QIODevice::ReadOnly);
174  stream >> info;
175  if (data->hasFormat("application/vnd+qtpass.dragAndDropInfoPasswordStore") ==
176  false)
177  return false;
178 
179  if (column > 0) {
180  return false;
181  }
182 
183  // you can drop a folder on a folder
184  if (fs->fileInfo(mapToSource(useIndex)).isDir() && info.isDir) {
185  return true;
186  }
187  // you can drop a file on a folder
188  if (fs->fileInfo(mapToSource(useIndex)).isDir() && info.isFile) {
189  return true;
190  }
191  // you can drop a file on a file
192  if (fs->fileInfo(mapToSource(useIndex)).isFile() && info.isFile) {
193  return true;
194  }
195 
196  return false;
197 }
198 
199 bool StoreModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
200  int row, int column, const QModelIndex &parent) {
201  if (!canDropMimeData(data, action, row, column, parent))
202  return false;
203 
204  if (action == Qt::IgnoreAction) {
205  return true;
206  }
207  QByteArray encodedData =
208  data->data("application/vnd+qtpass.dragAndDropInfoPasswordStore");
209 
210  QDataStream stream(&encodedData, QIODevice::ReadOnly);
212  stream >> info;
213  QModelIndex destIndex =
214  this->index(parent.row(), parent.column(), parent.parent());
215  QFileInfo destFileinfo = fs->fileInfo(mapToSource(destIndex));
216  QFileInfo srcFileInfo = QFileInfo(info.path);
217  QDir qdir;
218  QString cleanedSrc = qdir.cleanPath(srcFileInfo.absoluteFilePath());
219  QString cleanedDest = qdir.cleanPath(destFileinfo.absoluteFilePath());
220  if (info.isDir) {
221  QDir srcDir = QDir(info.path);
222  // dropped dir onto dir
223  if (destFileinfo.isDir()) {
224  QDir destDir = QDir(cleanedDest).filePath(srcFileInfo.fileName());
225  QString cleanedDestDir = qdir.cleanPath(destDir.absolutePath());
226  if (action == Qt::MoveAction) {
227  QtPassSettings::getPass()->Move(cleanedSrc, cleanedDestDir);
228  } else if (action == Qt::CopyAction) {
229  QtPassSettings::getPass()->Copy(cleanedSrc, cleanedDestDir);
230  }
231  }
232  } else if (info.isFile) {
233  // dropped file onto a directory
234  if (destFileinfo.isDir()) {
235  if (action == Qt::MoveAction) {
236  QtPassSettings::getPass()->Move(cleanedSrc, cleanedDest);
237  } else if (action == Qt::CopyAction) {
238  QtPassSettings::getPass()->Copy(cleanedSrc, cleanedDest);
239  }
240  } else if (destFileinfo.isFile()) {
241  // dropped file onto a file
242  int answer = QMessageBox::question(
243  0, tr("force overwrite?"),
244  tr("overwrite %1 with %2?").arg(cleanedDest).arg(cleanedSrc),
245  QMessageBox::Yes | QMessageBox::No);
246  bool force = answer == QMessageBox::Yes;
247  if (action == Qt::MoveAction) {
248  QtPassSettings::getPass()->Move(cleanedSrc, cleanedDest, force);
249  } else if (action == Qt::CopyAction) {
250  QtPassSettings::getPass()->Copy(cleanedSrc, cleanedDest, force);
251  }
252  }
253  }
254  return true;
255 }
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:199
QDataStream & operator>>(QDataStream &in, dragAndDropInfoPasswordStore &dragAndDropInfoPasswordStore)
Definition: storemodel.cpp:18