1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
|
/* This file is part of the KDE project
Copyright (C) 2007 Kevin Ottens <ervin@kde.org>
Copyright (C) 2007 David Faure <faure@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "kfileplacesmodel.h"
#include "kfileplacesitem_p.h"
#include "kfileplacessharedbookmarks_p.h"
#ifdef _WIN32_WCE
#include "Windows.h"
#include "WinBase.h"
#include <QtCore/QDir>
#endif
#include <QtCore/QMimeData>
#include <QtCore/QTimer>
#include <QtCore/QFile>
#include <QtGui/QColor>
#include <QtGui/QAction>
#include <kfileitem.h>
#include <kglobal.h>
#include <klocale.h>
#include <kuser.h>
#include <kstandarddirs.h>
#include <kcomponentdata.h>
#include <kicon.h>
#include <kmimetype.h>
#include <kdebug.h>
#include <kbookmarkmanager.h>
#include <kbookmark.h>
#include <kio/netaccess.h>
#include <kprotocolinfo.h>
#include <solid/devicenotifier.h>
#include <solid/storageaccess.h>
#include <solid/storagedrive.h>
#include <solid/storagevolume.h>
#include <solid/opticaldrive.h>
#include <solid/opticaldisc.h>
#include <solid/portablemediaplayer.h>
#include <solid/predicate.h>
class KFilePlacesModel::Private
{
public:
Private(KFilePlacesModel *self) : q(self), bookmarkManager(0), sharedBookmarks(0) {}
~Private()
{
delete sharedBookmarks;
qDeleteAll(items);
}
KFilePlacesModel *q;
QList<KFilePlacesItem*> items;
QSet<QString> availableDevices;
QMap<QObject*, QPersistentModelIndex> setupInProgress;
Solid::Predicate predicate;
KBookmarkManager *bookmarkManager;
KFilePlacesSharedBookmarks * sharedBookmarks;
void reloadAndSignal();
QList<KFilePlacesItem *> loadBookmarkList();
void _k_initDeviceList();
void _k_deviceAdded(const QString &udi);
void _k_deviceRemoved(const QString &udi);
void _k_itemChanged(const QString &udi);
void _k_reloadBookmarks();
void _k_storageSetupDone(Solid::ErrorType error, QVariant errorData);
void _k_storageTeardownDone(Solid::ErrorType error, QVariant errorData);
};
KFilePlacesModel::KFilePlacesModel(QObject *parent)
: QAbstractItemModel(parent), d(new Private(this))
{
const QString file = KStandardDirs::locateLocal("data", "kfileplaces/bookmarks.xml");
d->bookmarkManager = KBookmarkManager::managerForFile(file, "kfilePlaces");
// Let's put some places in there if it's empty. We have a corner case here:
// Given you have bookmarked some folders (which have been saved on
// ~/.local/share/user-places.xbel (according to freedesktop bookmarks spec), and
// deleted the home directory ~/.kde, the call managerForFile() will return the
// bookmark manager for the fallback "kfilePlaces", making root.first().isNull() being
// false (you have your own items bookmarked), resulting on only being added your own
// bookmarks, and not the default ones too. So, we also check if kfileplaces/bookmarks.xml
// file exists, and if it doesn't, we also add the default places. (ereslibre)
KBookmarkGroup root = d->bookmarkManager->root();
if (root.first().isNull() || !QFile::exists(file)) {
// NOTE: The context for these I18N_NOOP2 calls has to be "KFile System Bookmarks".
// The real i18nc call is made later, with this context, so the two must match.
//
// createSystemBookmark actually does nothing with its third argument,
// but we have to give it something so the I18N_NOOP2 calls stay here for now.
//
// (coles, 13th May 2009)
KFilePlacesItem::createSystemBookmark(d->bookmarkManager,
"Home", I18N_NOOP2("KFile System Bookmarks", "Home"),
KUrl(KUser().homeDir()), "user-home");
KFilePlacesItem::createSystemBookmark(d->bookmarkManager,
"Network", I18N_NOOP2("KFile System Bookmarks", "Network"),
KUrl("remote:/"), "network-workgroup");
#if defined(_WIN32_WCE)
// adding drives
foreach ( const QFileInfo& info, QDir::drives() ) {
QString driveIcon = "drive-harddisk";
KFilePlacesItem::createSystemBookmark(d->bookmarkManager,
info.absoluteFilePath(), info.absoluteFilePath(),
KUrl(info.absoluteFilePath()), driveIcon);
}
#elif !defined(Q_OS_WIN)
KFilePlacesItem::createSystemBookmark(d->bookmarkManager,
"Root", I18N_NOOP2("KFile System Bookmarks", "Root"),
KUrl("/"), "folder-red");
#endif
KFilePlacesItem::createSystemBookmark(d->bookmarkManager,
"Trash", I18N_NOOP2("KFile System Bookmarks", "Trash"),
KUrl("trash:/"), "user-trash");
// Force bookmarks to be saved. If on open/save dialog and the bookmarks are not saved, QFile::exists
// will always return false, which opening/closing all the time the open/save dialog would case the
// bookmarks to be added once each time, having lots of times each bookmark. This forces the defaults
// to be saved on the bookmarks.xml file. Of course, the complete list of bookmarks (those that come from
// user-places.xbel will be filled later). (ereslibre)
d->bookmarkManager->saveAs(file);
}
// create after, so if we have own places, they are added afterwards, in case of equal priorities
d->sharedBookmarks = new KFilePlacesSharedBookmarks(d->bookmarkManager);
QString predicate("[[[[ StorageVolume.ignored == false AND [ StorageVolume.usage == 'FileSystem' OR StorageVolume.usage == 'Encrypted' ]]"
" OR "
"[ IS StorageAccess AND StorageDrive.driveType == 'Floppy' ]]"
" OR "
"OpticalDisc.availableContent & 'Audio' ]"
" OR "
"StorageAccess.ignored == false ]");
if (KProtocolInfo::isKnownProtocol("mtp")) {
predicate.prepend("[");
predicate.append(" OR PortableMediaPlayer.supportedProtocols == 'mtp']");
}
d->predicate = Solid::Predicate::fromString(predicate);
Q_ASSERT(d->predicate.isValid());
connect(d->bookmarkManager, SIGNAL(changed(QString,QString)),
this, SLOT(_k_reloadBookmarks()));
connect(d->bookmarkManager, SIGNAL(bookmarksChanged(QString)),
this, SLOT(_k_reloadBookmarks()));
d->_k_reloadBookmarks();
QTimer::singleShot(0, this, SLOT(_k_initDeviceList()));
}
KFilePlacesModel::~KFilePlacesModel()
{
delete d;
}
KUrl KFilePlacesModel::url(const QModelIndex &index) const
{
return KUrl(data(index, UrlRole).toUrl());
}
bool KFilePlacesModel::setupNeeded(const QModelIndex &index) const
{
return data(index, SetupNeededRole).toBool();
}
KIcon KFilePlacesModel::icon(const QModelIndex &index) const
{
return KIcon(data(index, Qt::DecorationRole).value<QIcon>());
}
QString KFilePlacesModel::text(const QModelIndex &index) const
{
return data(index, Qt::DisplayRole).toString();
}
bool KFilePlacesModel::isHidden(const QModelIndex &index) const
{
return data(index, HiddenRole).toBool();
}
bool KFilePlacesModel::isDevice(const QModelIndex &index) const
{
if (!index.isValid())
return false;
KFilePlacesItem *item = static_cast<KFilePlacesItem*>(index.internalPointer());
return item->isDevice();
}
Solid::Device KFilePlacesModel::deviceForIndex(const QModelIndex &index) const
{
if (!index.isValid())
return Solid::Device();
KFilePlacesItem *item = static_cast<KFilePlacesItem*>(index.internalPointer());
if (item->isDevice()) {
return item->device();
} else {
return Solid::Device();
}
}
KBookmark KFilePlacesModel::bookmarkForIndex(const QModelIndex &index) const
{
if (!index.isValid())
return KBookmark();
KFilePlacesItem *item = static_cast<KFilePlacesItem*>(index.internalPointer());
if (!item->isDevice()) {
return item->bookmark();
} else {
return KBookmark();
}
}
QVariant KFilePlacesModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
KFilePlacesItem *item = static_cast<KFilePlacesItem*>(index.internalPointer());
return item->data(role);
}
QModelIndex KFilePlacesModel::index(int row, int column, const QModelIndex &parent) const
{
if (row<0 || column!=0 || row>=d->items.size())
return QModelIndex();
if (parent.isValid())
return QModelIndex();
return createIndex(row, column, d->items.at(row));
}
QModelIndex KFilePlacesModel::parent(const QModelIndex &child) const
{
Q_UNUSED(child);
return QModelIndex();
}
int KFilePlacesModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
else
return d->items.size();
}
int KFilePlacesModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
// We only know 1 piece of information for a particular entry
return 1;
}
QModelIndex KFilePlacesModel::closestItem(const KUrl &url) const
{
int foundRow = -1;
int maxLength = 0;
// Search the item which is equal to the URL or at least is a parent URL.
// If there are more than one possible item URL candidates, choose the item
// which covers the bigger range of the URL.
for (int row = 0; row<d->items.size(); ++row) {
KFilePlacesItem *item = d->items[row];
KUrl itemUrl = KUrl(item->data(UrlRole).toUrl());
if (itemUrl.isParentOf(url)) {
const int length = itemUrl.prettyUrl().length();
if (length > maxLength) {
foundRow = row;
maxLength = length;
}
}
}
if (foundRow==-1)
return QModelIndex();
else
return createIndex(foundRow, 0, d->items[foundRow]);
}
void KFilePlacesModel::Private::_k_initDeviceList()
{
Solid::DeviceNotifier *notifier = Solid::DeviceNotifier::instance();
connect(notifier, SIGNAL(deviceAdded(QString)),
q, SLOT(_k_deviceAdded(QString)));
connect(notifier, SIGNAL(deviceRemoved(QString)),
q, SLOT(_k_deviceRemoved(QString)));
const QList<Solid::Device> &deviceList = Solid::Device::listFromQuery(predicate);
foreach(const Solid::Device &device, deviceList) {
availableDevices << device.udi();
}
_k_reloadBookmarks();
}
void KFilePlacesModel::Private::_k_deviceAdded(const QString &udi)
{
Solid::Device d(udi);
if (predicate.matches(d)) {
availableDevices << udi;
_k_reloadBookmarks();
}
}
void KFilePlacesModel::Private::_k_deviceRemoved(const QString &udi)
{
if (availableDevices.contains(udi)) {
availableDevices.remove(udi);
_k_reloadBookmarks();
}
}
void KFilePlacesModel::Private::_k_itemChanged(const QString &id)
{
for (int row = 0; row<items.size(); ++row) {
if (items.at(row)->id()==id) {
QModelIndex index = q->index(row, 0);
emit q->dataChanged(index, index);
}
}
}
void KFilePlacesModel::Private::_k_reloadBookmarks()
{
QList<KFilePlacesItem*> currentItems = loadBookmarkList();
QList<KFilePlacesItem*>::Iterator it_i = items.begin();
QList<KFilePlacesItem*>::Iterator it_c = currentItems.begin();
QList<KFilePlacesItem*>::Iterator end_i = items.end();
QList<KFilePlacesItem*>::Iterator end_c = currentItems.end();
while (it_i!=end_i || it_c!=end_c) {
if (it_i==end_i && it_c!=end_c) {
int row = items.count();
q->beginInsertRows(QModelIndex(), row, row);
it_i = items.insert(it_i, *it_c);
++it_i;
it_c = currentItems.erase(it_c);
end_i = items.end();
end_c = currentItems.end();
q->endInsertRows();
} else if (it_i!=end_i && it_c==end_c) {
int row = items.indexOf(*it_i);
q->beginRemoveRows(QModelIndex(), row, row);
delete *it_i;
it_i = items.erase(it_i);
end_i = items.end();
end_c = currentItems.end();
q->endRemoveRows();
} else if ((*it_i)->id()==(*it_c)->id()) {
bool shouldEmit = !((*it_i)->bookmark()==(*it_c)->bookmark());
(*it_i)->setBookmark((*it_c)->bookmark());
if (shouldEmit) {
int row = items.indexOf(*it_i);
QModelIndex idx = q->index(row, 0);
emit q->dataChanged(idx, idx);
}
++it_i;
++it_c;
} else if ((*it_i)->id()!=(*it_c)->id()) {
int row = items.indexOf(*it_i);
if (it_i+1!=end_i && (*(it_i+1))->id()==(*it_c)->id()) { // if the next one matches, it's a remove
q->beginRemoveRows(QModelIndex(), row, row);
delete *it_i;
it_i = items.erase(it_i);
end_i = items.end();
end_c = currentItems.end();
q->endRemoveRows();
} else {
q->beginInsertRows(QModelIndex(), row, row);
it_i = items.insert(it_i, *it_c);
++it_i;
it_c = currentItems.erase(it_c);
end_i = items.end();
end_c = currentItems.end();
q->endInsertRows();
}
}
}
qDeleteAll(currentItems);
currentItems.clear();
}
QList<KFilePlacesItem *> KFilePlacesModel::Private::loadBookmarkList()
{
QList<KFilePlacesItem*> items;
KBookmarkGroup root = bookmarkManager->root();
KBookmark bookmark = root.first();
QSet<QString> devices = availableDevices;
while (!bookmark.isNull()) {
QString udi = bookmark.metaDataItem("UDI");
QString appName = bookmark.metaDataItem("OnlyInApp");
bool deviceAvailable = devices.remove(udi);
bool allowedHere = appName.isEmpty() || (appName==KGlobal::mainComponent().componentName());
if ((udi.isEmpty() && allowedHere) || deviceAvailable) {
KFilePlacesItem *item;
if (deviceAvailable) {
item = new KFilePlacesItem(bookmarkManager, bookmark.address(), udi);
// TODO: Update bookmark internal element
} else {
item = new KFilePlacesItem(bookmarkManager, bookmark.address());
}
connect(item, SIGNAL(itemChanged(QString)),
q, SLOT(_k_itemChanged(QString)));
items << item;
}
bookmark = root.next(bookmark);
}
// Add bookmarks for the remaining devices, they were previously unknown
foreach (const QString &udi, devices) {
bookmark = KFilePlacesItem::createDeviceBookmark(bookmarkManager, udi);
if (!bookmark.isNull()) {
KFilePlacesItem *item = new KFilePlacesItem(bookmarkManager,
bookmark.address(), udi);
connect(item, SIGNAL(itemChanged(QString)),
q, SLOT(_k_itemChanged(QString)));
// TODO: Update bookmark internal element
items << item;
}
}
return items;
}
void KFilePlacesModel::Private::reloadAndSignal()
{
bookmarkManager->emitChanged(bookmarkManager->root()); // ... we'll get relisted anyway
}
Qt::DropActions KFilePlacesModel::supportedDropActions() const
{
return Qt::ActionMask;
}
Qt::ItemFlags KFilePlacesModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags res = Qt::ItemIsSelectable|Qt::ItemIsEnabled;
if (index.isValid())
res|= Qt::ItemIsDragEnabled;
if (!index.isValid())
res|= Qt::ItemIsDropEnabled;
return res;
}
static QString _k_internalMimetype(const KFilePlacesModel * const self)
{
return QString("application/x-kfileplacesmodel-")+QString::number((qptrdiff)self);
}
QStringList KFilePlacesModel::mimeTypes() const
{
QStringList types;
types << _k_internalMimetype(this) << "text/uri-list";
return types;
}
QMimeData *KFilePlacesModel::mimeData(const QModelIndexList &indexes) const
{
KUrl::List urls;
QByteArray itemData;
QDataStream stream(&itemData, QIODevice::WriteOnly);
foreach (const QModelIndex &index, indexes) {
KUrl itemUrl = url(index);
if (itemUrl.isValid())
urls << itemUrl;
stream << index.row();
}
QMimeData *mimeData = new QMimeData();
if (!urls.isEmpty())
urls.populateMimeData(mimeData);
mimeData->setData(_k_internalMimetype(this), itemData);
return mimeData;
}
bool KFilePlacesModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
int row, int column, const QModelIndex &parent)
{
if (action == Qt::IgnoreAction)
return true;
if (column > 0)
return false;
if (row==-1 && parent.isValid()) {
return false; // Don't allow to move an item onto another one,
// too easy for the user to mess something up
// If we really really want to allow copying files this way,
// let's do it in the views to get the good old drop menu
}
KBookmark afterBookmark;
if (row==-1) {
// The dropped item is moved or added to the last position
KFilePlacesItem *lastItem = d->items.last();
afterBookmark = lastItem->bookmark();
} else {
// The dropped item is moved or added before position 'row', ie after position 'row-1'
if (row>0) {
KFilePlacesItem *afterItem = d->items[row-1];
afterBookmark = afterItem->bookmark();
}
}
if (data->hasFormat(_k_internalMimetype(this))) {
// The operation is an internal move
QByteArray itemData = data->data(_k_internalMimetype(this));
QDataStream stream(&itemData, QIODevice::ReadOnly);
int itemRow;
stream >> itemRow;
KFilePlacesItem *item = d->items[itemRow];
KBookmark bookmark = item->bookmark();
int destRow = row == -1 ? d->items.count() : row;
// The item is not moved when the drop indicator is on either item edge
if (itemRow == destRow || itemRow + 1 == destRow) {
return false;
}
beginMoveRows(QModelIndex(), itemRow, itemRow, QModelIndex(), destRow);
d->bookmarkManager->root().moveBookmark(bookmark, afterBookmark);
// Move item ourselves so that _k_reloadBookmarks() does not consider
// the move as a remove + insert.
//
// 2nd argument of QList::move() expects the final destination index,
// but 'row' is the value of the destination index before the moved
// item has been removed from its original position. That is why we
// adjust if necessary.
d->items.move(itemRow, itemRow < destRow ? (destRow - 1) : destRow);
endMoveRows();
} else if (data->hasFormat("text/uri-list")) {
// The operation is an add
KUrl::List urls = KUrl::List::fromMimeData(data);
KBookmarkGroup group = d->bookmarkManager->root();
foreach (const KUrl &url, urls) {
// TODO: use KIO::stat in order to get the UDS_DISPLAY_NAME too
KMimeType::Ptr mimetype = KMimeType::mimeType(KIO::NetAccess::mimetype(url, 0));
if (!mimetype) {
kWarning() << "URL not added to Places as mimetype could not be determined!";
continue;
}
if (!mimetype->is("inode/directory")) {
// Only directories are allowed
continue;
}
KBookmark bookmark = KFilePlacesItem::createBookmark(d->bookmarkManager,
url.fileName(), url,
mimetype->iconName(url));
group.moveBookmark(bookmark, afterBookmark);
afterBookmark = bookmark;
}
} else {
// Oops, shouldn't happen thanks to mimeTypes()
kWarning() << ": received wrong mimedata, " << data->formats();
return false;
}
d->reloadAndSignal();
return true;
}
void KFilePlacesModel::addPlace(const QString &text, const KUrl &url,
const QString &iconName, const QString &appName)
{
addPlace(text, url, iconName, appName, QModelIndex());
}
void KFilePlacesModel::addPlace(const QString &text, const KUrl &url,
const QString &iconName, const QString &appName,
const QModelIndex &after)
{
KBookmark bookmark = KFilePlacesItem::createBookmark(d->bookmarkManager,
text, url, iconName);
if (!appName.isEmpty()) {
bookmark.setMetaDataItem("OnlyInApp", appName);
}
if (after.isValid()) {
KFilePlacesItem *item = static_cast<KFilePlacesItem*>(after.internalPointer());
d->bookmarkManager->root().moveBookmark(bookmark, item->bookmark());
}
d->reloadAndSignal();
}
void KFilePlacesModel::editPlace(const QModelIndex &index, const QString &text, const KUrl &url,
const QString &iconName, const QString &appName)
{
if (!index.isValid()) return;
KFilePlacesItem *item = static_cast<KFilePlacesItem*>(index.internalPointer());
if (item->isDevice()) return;
KBookmark bookmark = item->bookmark();
if (bookmark.isNull()) return;
bookmark.setFullText(text);
bookmark.setUrl(url);
bookmark.setIcon(iconName);
bookmark.setMetaDataItem("OnlyInApp", appName);
d->reloadAndSignal();
emit dataChanged(index, index);
}
void KFilePlacesModel::removePlace(const QModelIndex &index) const
{
if (!index.isValid()) return;
KFilePlacesItem *item = static_cast<KFilePlacesItem*>(index.internalPointer());
if (item->isDevice()) return;
KBookmark bookmark = item->bookmark();
if (bookmark.isNull()) return;
d->bookmarkManager->root().deleteBookmark(bookmark);
d->reloadAndSignal();
}
void KFilePlacesModel::setPlaceHidden(const QModelIndex &index, bool hidden)
{
if (!index.isValid()) return;
KFilePlacesItem *item = static_cast<KFilePlacesItem*>(index.internalPointer());
KBookmark bookmark = item->bookmark();
if (bookmark.isNull()) return;
bookmark.setMetaDataItem("IsHidden", (hidden ? "true" : "false"));
d->reloadAndSignal();
emit dataChanged(index, index);
}
int KFilePlacesModel::hiddenCount() const
{
int rows = rowCount();
int hidden = 0;
for (int i=0; i<rows; ++i) {
if (isHidden(index(i, 0))) {
hidden++;
}
}
return hidden;
}
QAction *KFilePlacesModel::teardownActionForIndex(const QModelIndex &index) const
{
Solid::Device device = deviceForIndex(index);
if (device.is<Solid::StorageAccess>() && device.as<Solid::StorageAccess>()->isAccessible()) {
Solid::StorageDrive *drive = device.as<Solid::StorageDrive>();
if (drive==0) {
drive = device.parent().as<Solid::StorageDrive>();
}
bool hotpluggable = false;
bool removable = false;
if (drive!=0) {
hotpluggable = drive->isHotpluggable();
removable = drive->isRemovable();
}
QString iconName;
QString text;
QString label = data(index, Qt::DisplayRole).toString().replace('&',"&&");
if (device.is<Solid::OpticalDisc>()) {
text = i18n("&Release '%1'", label);
} else if (removable || hotpluggable) {
text = i18n("&Safely Remove '%1'", label);
iconName = "media-eject";
} else {
text = i18n("&Unmount '%1'", label);
iconName = "media-eject";
}
if (!iconName.isEmpty()) {
return new QAction(KIcon(iconName), text, 0);
} else {
return new QAction(text, 0);
}
}
return 0;
}
QAction *KFilePlacesModel::ejectActionForIndex(const QModelIndex &index) const
{
Solid::Device device = deviceForIndex(index);
if (device.is<Solid::OpticalDisc>()) {
QString label = data(index, Qt::DisplayRole).toString().replace('&',"&&");
QString text = i18n("&Eject '%1'", label);
return new QAction(KIcon("media-eject"), text, 0);
}
return 0;
}
void KFilePlacesModel::requestTeardown(const QModelIndex &index)
{
Solid::Device device = deviceForIndex(index);
Solid::StorageAccess *access = device.as<Solid::StorageAccess>();
if (access!=0) {
connect(access, SIGNAL(teardownDone(Solid::ErrorType,QVariant,QString)),
this, SLOT(_k_storageTeardownDone(Solid::ErrorType,QVariant)));
access->teardown();
}
}
void KFilePlacesModel::requestEject(const QModelIndex &index)
{
Solid::Device device = deviceForIndex(index);
Solid::OpticalDrive *drive = device.parent().as<Solid::OpticalDrive>();
if (drive!=0) {
connect(drive, SIGNAL(ejectDone(Solid::ErrorType,QVariant,QString)),
this, SLOT(_k_storageTeardownDone(Solid::ErrorType,QVariant)));
drive->eject();
} else {
QString label = data(index, Qt::DisplayRole).toString().replace('&',"&&");
QString message = i18n("The device '%1' is not a disk and cannot be ejected.", label);
emit errorMessage(message);
}
}
void KFilePlacesModel::requestSetup(const QModelIndex &index)
{
Solid::Device device = deviceForIndex(index);
if (device.is<Solid::StorageAccess>()
&& !d->setupInProgress.contains(device.as<Solid::StorageAccess>())
&& !device.as<Solid::StorageAccess>()->isAccessible()) {
Solid::StorageAccess *access = device.as<Solid::StorageAccess>();
d->setupInProgress[access] = index;
connect(access, SIGNAL(setupDone(Solid::ErrorType,QVariant,QString)),
this, SLOT(_k_storageSetupDone(Solid::ErrorType,QVariant)));
access->setup();
}
}
void KFilePlacesModel::Private::_k_storageSetupDone(Solid::ErrorType error, QVariant errorData)
{
QPersistentModelIndex index = setupInProgress.take(q->sender());
if (!index.isValid()) {
return;
}
if (!error) {
emit q->setupDone(index, true);
} else {
if (errorData.isValid()) {
emit q->errorMessage(i18n("An error occurred while accessing '%1', the system responded: %2",
q->text(index),
errorData.toString()));
} else {
emit q->errorMessage(i18n("An error occurred while accessing '%1'",
q->text(index)));
}
emit q->setupDone(index, false);
}
}
void KFilePlacesModel::Private::_k_storageTeardownDone(Solid::ErrorType error, QVariant errorData)
{
if (error && errorData.isValid()) {
emit q->errorMessage(errorData.toString());
}
}
#include "kfileplacesmodel.moc"
|