mirror of
https://invent.kde.org/system/dolphin
synced 2024-09-19 16:31:21 +00:00
Implement bookmark synchronization
Whenever the bookmarks are changed by another application, the state must be synchronized within the model.
This commit is contained in:
parent
d47d83c9ae
commit
88c6859284
|
@ -63,7 +63,14 @@ PlacesItem::~PlacesItem()
|
||||||
|
|
||||||
void PlacesItem::setUrl(const KUrl& url)
|
void PlacesItem::setUrl(const KUrl& url)
|
||||||
{
|
{
|
||||||
setDataValue("url", url);
|
// The default check in KStandardItem::setDataValue()
|
||||||
|
// for equal values does not work with a custom value
|
||||||
|
// like KUrl. Hence do a manual check to prevent that
|
||||||
|
// setting an equal URL results in an itemsChanged()
|
||||||
|
// signal.
|
||||||
|
if (dataValue("url").value<KUrl>() != url) {
|
||||||
|
setDataValue("url", url);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
KUrl PlacesItem::url() const
|
KUrl PlacesItem::url() const
|
||||||
|
@ -84,9 +91,6 @@ QString PlacesItem::udi() const
|
||||||
void PlacesItem::setHidden(bool hidden)
|
void PlacesItem::setHidden(bool hidden)
|
||||||
{
|
{
|
||||||
setDataValue("isHidden", hidden);
|
setDataValue("isHidden", hidden);
|
||||||
if (!m_bookmark.isNull()) {
|
|
||||||
m_bookmark.setMetaDataItem("IsHidden", hidden ? "true" : "false");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PlacesItem::isHidden() const
|
bool PlacesItem::isHidden() const
|
||||||
|
@ -121,9 +125,8 @@ void PlacesItem::setBookmark(const KBookmark& bookmark)
|
||||||
const QString udi = bookmark.metaDataItem("UDI");
|
const QString udi = bookmark.metaDataItem("UDI");
|
||||||
if (udi.isEmpty()) {
|
if (udi.isEmpty()) {
|
||||||
setIcon(bookmark.icon());
|
setIcon(bookmark.icon());
|
||||||
setText(bookmark.description());
|
setText(bookmark.text());
|
||||||
setUrl(bookmark.url());
|
setUrl(bookmark.url());
|
||||||
setDataValue("address", bookmark.address());
|
|
||||||
} else {
|
} else {
|
||||||
initializeDevice(udi);
|
initializeDevice(udi);
|
||||||
}
|
}
|
||||||
|
@ -173,7 +176,7 @@ KBookmark PlacesItem::createBookmark(KBookmarkManager* manager,
|
||||||
}
|
}
|
||||||
|
|
||||||
KBookmark bookmark = root.addBookmark(text, url, iconName);
|
KBookmark bookmark = root.addBookmark(text, url, iconName);
|
||||||
bookmark.setDescription(text);
|
bookmark.setFullText(text);
|
||||||
bookmark.setMetaDataItem("ID", generateNewId());
|
bookmark.setMetaDataItem("ID", generateNewId());
|
||||||
|
|
||||||
return bookmark;
|
return bookmark;
|
||||||
|
@ -259,7 +262,7 @@ void PlacesItem::updateBookmarkForRole(const QByteArray& role)
|
||||||
if (role == "iconName") {
|
if (role == "iconName") {
|
||||||
m_bookmark.setIcon(icon());
|
m_bookmark.setIcon(icon());
|
||||||
} else if (role == "text") {
|
} else if (role == "text") {
|
||||||
m_bookmark.setDescription(text());
|
m_bookmark.setFullText(text());
|
||||||
} else if (role == "url") {
|
} else if (role == "url") {
|
||||||
m_bookmark.setUrl(url());
|
m_bookmark.setUrl(url());
|
||||||
} else if (role == "udi)") {
|
} else if (role == "udi)") {
|
||||||
|
@ -273,6 +276,11 @@ void PlacesItem::updateBookmarkForRole(const QByteArray& role)
|
||||||
|
|
||||||
QString PlacesItem::generateNewId()
|
QString PlacesItem::generateNewId()
|
||||||
{
|
{
|
||||||
|
// The ID-generation must be different as done in KFilePlacesItem from kdelibs
|
||||||
|
// to prevent identical IDs, because 'count' is of course not shared. We append a
|
||||||
|
// " (V2)" to indicate that the ID has been generated by
|
||||||
|
// a new version of the places view.
|
||||||
static int count = 0;
|
static int count = 0;
|
||||||
return QString::number(QDateTime::currentDateTime().toTime_t()) + '/' + QString::number(count++);
|
return QString::number(QDateTime::currentDateTime().toTime_t()) +
|
||||||
|
'/' + QString::number(count++) + " (V2)";
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,6 +48,17 @@
|
||||||
#include <Nepomuk/ResourceManager>
|
#include <Nepomuk/ResourceManager>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
// As long as KFilePlacesView from kdelibs is available in parallel, the
|
||||||
|
// system-bookmarks for "Recently Accessed" and "Search For" should be
|
||||||
|
// shown only inside the Places Panel. This is necessary as the stored
|
||||||
|
// URLs needs to get translated to a Nepomuk-search-URL on-the-fly to
|
||||||
|
// be independent from changes in the Nepomuk-search-URL-syntax.
|
||||||
|
// Hence a prefix to the application-name of the stored bookmarks is
|
||||||
|
// added, which is only read by PlacesItemModel.
|
||||||
|
const char* AppNamePrefix = "-places-panel";
|
||||||
|
}
|
||||||
|
|
||||||
PlacesItemModel::PlacesItemModel(QObject* parent) :
|
PlacesItemModel::PlacesItemModel(QObject* parent) :
|
||||||
KStandardItemModel(parent),
|
KStandardItemModel(parent),
|
||||||
m_nepomukRunning(false),
|
m_nepomukRunning(false),
|
||||||
|
@ -57,9 +68,10 @@ PlacesItemModel::PlacesItemModel(QObject* parent) :
|
||||||
m_bookmarkManager(0),
|
m_bookmarkManager(0),
|
||||||
m_systemBookmarks(),
|
m_systemBookmarks(),
|
||||||
m_systemBookmarksIndexes(),
|
m_systemBookmarksIndexes(),
|
||||||
m_hiddenItems(),
|
m_bookmarkedItems(),
|
||||||
m_hiddenItemToRemove(-1),
|
m_hiddenItemToRemove(-1),
|
||||||
m_saveBookmarksTimer(0)
|
m_saveBookmarksTimer(0),
|
||||||
|
m_updateBookmarksTimer(0)
|
||||||
{
|
{
|
||||||
#ifdef HAVE_NEPOMUK
|
#ifdef HAVE_NEPOMUK
|
||||||
m_nepomukRunning = (Nepomuk::ResourceManager::instance()->initialized());
|
m_nepomukRunning = (Nepomuk::ResourceManager::instance()->initialized());
|
||||||
|
@ -71,17 +83,29 @@ PlacesItemModel::PlacesItemModel(QObject* parent) :
|
||||||
initializeAvailableDevices();
|
initializeAvailableDevices();
|
||||||
loadBookmarks();
|
loadBookmarks();
|
||||||
|
|
||||||
|
const int syncBookmarksTimeout = 1000;
|
||||||
|
|
||||||
m_saveBookmarksTimer = new QTimer(this);
|
m_saveBookmarksTimer = new QTimer(this);
|
||||||
m_saveBookmarksTimer->setInterval(100);
|
m_saveBookmarksTimer->setInterval(syncBookmarksTimeout);
|
||||||
m_saveBookmarksTimer->setSingleShot(true);
|
m_saveBookmarksTimer->setSingleShot(true);
|
||||||
connect(m_saveBookmarksTimer, SIGNAL(timeout()), this, SLOT(saveBookmarks()));
|
connect(m_saveBookmarksTimer, SIGNAL(timeout()), this, SLOT(saveBookmarks()));
|
||||||
|
|
||||||
|
m_updateBookmarksTimer = new QTimer(this);
|
||||||
|
m_updateBookmarksTimer->setInterval(syncBookmarksTimeout);
|
||||||
|
m_updateBookmarksTimer->setSingleShot(true);
|
||||||
|
connect(m_updateBookmarksTimer, SIGNAL(timeout()), this, SLOT(updateBookmarks()));
|
||||||
|
|
||||||
|
connect(m_bookmarkManager, SIGNAL(changed(QString,QString)),
|
||||||
|
m_updateBookmarksTimer, SLOT(start()));
|
||||||
|
connect(m_bookmarkManager, SIGNAL(bookmarksChanged(QString)),
|
||||||
|
m_updateBookmarksTimer, SLOT(start()));
|
||||||
}
|
}
|
||||||
|
|
||||||
PlacesItemModel::~PlacesItemModel()
|
PlacesItemModel::~PlacesItemModel()
|
||||||
{
|
{
|
||||||
saveBookmarks();
|
saveBookmarks();
|
||||||
qDeleteAll(m_hiddenItems);
|
qDeleteAll(m_bookmarkedItems);
|
||||||
m_hiddenItems.clear();
|
m_bookmarkedItems.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
PlacesItem* PlacesItemModel::createPlacesItem(const QString& text,
|
PlacesItem* PlacesItemModel::createPlacesItem(const QString& text,
|
||||||
|
@ -100,19 +124,19 @@ PlacesItem* PlacesItemModel::placesItem(int index) const
|
||||||
int PlacesItemModel::hiddenCount() const
|
int PlacesItemModel::hiddenCount() const
|
||||||
{
|
{
|
||||||
int modelIndex = 0;
|
int modelIndex = 0;
|
||||||
int itemCount = 0;
|
int hiddenItemCount = 0;
|
||||||
foreach (const PlacesItem* hiddenItem, m_hiddenItems) {
|
foreach (const PlacesItem* item, m_bookmarkedItems) {
|
||||||
if (hiddenItem) {
|
if (item) {
|
||||||
++itemCount;
|
++hiddenItemCount;
|
||||||
} else {
|
} else {
|
||||||
if (placesItem(modelIndex)->isHidden()) {
|
if (placesItem(modelIndex)->isHidden()) {
|
||||||
++itemCount;
|
++hiddenItemCount;
|
||||||
}
|
}
|
||||||
++modelIndex;
|
++modelIndex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return itemCount;
|
return hiddenItemCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlacesItemModel::setHiddenItemsShown(bool show)
|
void PlacesItemModel::setHiddenItemsShown(bool show)
|
||||||
|
@ -124,26 +148,26 @@ void PlacesItemModel::setHiddenItemsShown(bool show)
|
||||||
m_hiddenItemsShown = show;
|
m_hiddenItemsShown = show;
|
||||||
|
|
||||||
if (show) {
|
if (show) {
|
||||||
// Move all items that are part of m_hiddenItems to the model.
|
// Move all items that are part of m_bookmarkedItems to the model.
|
||||||
int modelIndex = 0;
|
int modelIndex = 0;
|
||||||
for (int hiddenIndex = 0; hiddenIndex < m_hiddenItems.count(); ++hiddenIndex) {
|
for (int i = 0; i < m_bookmarkedItems.count(); ++i) {
|
||||||
if (m_hiddenItems[hiddenIndex]) {
|
if (m_bookmarkedItems[i]) {
|
||||||
PlacesItem* visibleItem = new PlacesItem(*m_hiddenItems[hiddenIndex]);
|
PlacesItem* visibleItem = new PlacesItem(*m_bookmarkedItems[i]);
|
||||||
delete m_hiddenItems[hiddenIndex];
|
delete m_bookmarkedItems[i];
|
||||||
m_hiddenItems.removeAt(hiddenIndex);
|
m_bookmarkedItems.removeAt(i);
|
||||||
insertItem(modelIndex, visibleItem);
|
insertItem(modelIndex, visibleItem);
|
||||||
Q_ASSERT(!m_hiddenItems[hiddenIndex]);
|
Q_ASSERT(!m_bookmarkedItems[i]);
|
||||||
}
|
}
|
||||||
++modelIndex;
|
++modelIndex;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Move all items of the model, where the "isHidden" property is true, to
|
// Move all items of the model, where the "isHidden" property is true, to
|
||||||
// m_hiddenItems.
|
// m_allItems.
|
||||||
Q_ASSERT(m_hiddenItems.count() == count());
|
Q_ASSERT(m_bookmarkedItems.count() == count());
|
||||||
for (int i = count() - 1; i >= 0; --i) {
|
for (int i = count() - 1; i >= 0; --i) {
|
||||||
const PlacesItem* visibleItem = placesItem(i);
|
const PlacesItem* visibleItem = placesItem(i);
|
||||||
if (visibleItem->isHidden()) {
|
if (visibleItem->isHidden()) {
|
||||||
removeHiddenItem(i);
|
hideItem(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -286,22 +310,22 @@ void PlacesItemModel::onItemInserted(int index)
|
||||||
// The item has been appended as last item to the list. In this
|
// The item has been appended as last item to the list. In this
|
||||||
// case assure that it is also appended after the hidden items and
|
// case assure that it is also appended after the hidden items and
|
||||||
// not before (like done otherwise).
|
// not before (like done otherwise).
|
||||||
m_hiddenItems.append(0);
|
m_bookmarkedItems.append(0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int modelIndex = -1;
|
int modelIndex = -1;
|
||||||
int hiddenIndex = 0;
|
int bookmarkIndex = 0;
|
||||||
while (hiddenIndex < m_hiddenItems.count()) {
|
while (bookmarkIndex < m_bookmarkedItems.count()) {
|
||||||
if (!m_hiddenItems[hiddenIndex]) {
|
if (!m_bookmarkedItems[bookmarkIndex]) {
|
||||||
++modelIndex;
|
++modelIndex;
|
||||||
if (modelIndex + 1 == index) {
|
if (modelIndex + 1 == index) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
++hiddenIndex;
|
++bookmarkIndex;
|
||||||
}
|
}
|
||||||
m_hiddenItems.insert(hiddenIndex, 0);
|
m_bookmarkedItems.insert(bookmarkIndex, 0);
|
||||||
|
|
||||||
m_saveBookmarksTimer->start();
|
m_saveBookmarksTimer->start();
|
||||||
|
|
||||||
|
@ -319,9 +343,9 @@ void PlacesItemModel::onItemRemoved(int index, KStandardItem* removedItem)
|
||||||
m_bookmarkManager->root().deleteBookmark(bookmark);
|
m_bookmarkManager->root().deleteBookmark(bookmark);
|
||||||
}
|
}
|
||||||
|
|
||||||
const int removeIndex = hiddenIndex(index);
|
const int boomarkIndex = bookmarkIndex(index);
|
||||||
Q_ASSERT(!m_hiddenItems[removeIndex]);
|
Q_ASSERT(!m_bookmarkedItems[boomarkIndex]);
|
||||||
m_hiddenItems.removeAt(removeIndex);
|
m_bookmarkedItems.removeAt(boomarkIndex);
|
||||||
|
|
||||||
m_saveBookmarksTimer->start();
|
m_saveBookmarksTimer->start();
|
||||||
|
|
||||||
|
@ -353,9 +377,10 @@ void PlacesItemModel::onItemChanged(int index, const QSet<QByteArray>& changedRo
|
||||||
Q_ASSERT(shownItem);
|
Q_ASSERT(shownItem);
|
||||||
if (!m_hiddenItemsShown && shownItem->isHidden()) {
|
if (!m_hiddenItemsShown && shownItem->isHidden()) {
|
||||||
m_hiddenItemToRemove = index;
|
m_hiddenItemToRemove = index;
|
||||||
QTimer::singleShot(0, this, SLOT(removeHiddenItem()));
|
QTimer::singleShot(0, this, SLOT(hideItem()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m_saveBookmarksTimer->start();
|
m_saveBookmarksTimer->start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -375,10 +400,10 @@ void PlacesItemModel::slotDeviceRemoved(const QString& udi)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < m_hiddenItems.count(); ++i) {
|
for (int i = 0; i < m_bookmarkedItems.count(); ++i) {
|
||||||
PlacesItem* item = m_hiddenItems[i];
|
PlacesItem* item = m_bookmarkedItems[i];
|
||||||
if (item && item->udi() == udi) {
|
if (item && item->udi() == udi) {
|
||||||
m_hiddenItems.removeAt(i);
|
m_bookmarkedItems.removeAt(i);
|
||||||
delete item;
|
delete item;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -399,17 +424,94 @@ void PlacesItemModel::slotStorageTeardownDone(Solid::ErrorType error, const QVar
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlacesItemModel::removeHiddenItem()
|
void PlacesItemModel::hideItem()
|
||||||
{
|
{
|
||||||
removeHiddenItem(m_hiddenItemToRemove);
|
hideItem(m_hiddenItemToRemove);
|
||||||
m_hiddenItemToRemove = -1;
|
m_hiddenItemToRemove = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PlacesItemModel::updateBookmarks()
|
||||||
|
{
|
||||||
|
// Verify whether new bookmarks have been added or existing
|
||||||
|
// bookmarks have been changed.
|
||||||
|
KBookmarkGroup root = m_bookmarkManager->root();
|
||||||
|
KBookmark newBookmark = root.first();
|
||||||
|
while (!newBookmark.isNull()) {
|
||||||
|
if (acceptBookmark(newBookmark)) {
|
||||||
|
bool found = false;
|
||||||
|
int modelIndex = 0;
|
||||||
|
for (int i = 0; i < m_bookmarkedItems.count(); ++i) {
|
||||||
|
PlacesItem* item = m_bookmarkedItems[i];
|
||||||
|
if (!item) {
|
||||||
|
item = placesItem(modelIndex);
|
||||||
|
++modelIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
const KBookmark oldBookmark = item->bookmark();
|
||||||
|
if (equalBookmarkIdentifiers(newBookmark, oldBookmark)) {
|
||||||
|
// The bookmark has been found in the model or as
|
||||||
|
// a hidden item. The content of the bookmark might
|
||||||
|
// have been changed, so an update is done.
|
||||||
|
found = true;
|
||||||
|
if (newBookmark.metaDataItem("UDI").isEmpty()) {
|
||||||
|
item->setBookmark(newBookmark);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!found) {
|
||||||
|
PlacesItem* item = new PlacesItem(newBookmark);
|
||||||
|
if (item->isHidden() && !m_hiddenItemsShown) {
|
||||||
|
m_bookmarkedItems.append(item);
|
||||||
|
} else {
|
||||||
|
appendItem(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
newBookmark = root.next(newBookmark);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove items that are not part of the bookmark-manager anymore
|
||||||
|
int modelIndex = 0;
|
||||||
|
for (int i = m_bookmarkedItems.count() - 1; i >= 0; --i) {
|
||||||
|
PlacesItem* item = m_bookmarkedItems[i];
|
||||||
|
const bool itemIsPartOfModel = (item == 0);
|
||||||
|
if (itemIsPartOfModel) {
|
||||||
|
item = placesItem(modelIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool hasBeenRemoved = true;
|
||||||
|
const KBookmark oldBookmark = item->bookmark();
|
||||||
|
KBookmark newBookmark = root.first();
|
||||||
|
while (!newBookmark.isNull()) {
|
||||||
|
if (equalBookmarkIdentifiers(newBookmark, oldBookmark)) {
|
||||||
|
hasBeenRemoved = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
newBookmark = root.next(newBookmark);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hasBeenRemoved) {
|
||||||
|
if (m_bookmarkedItems[i]) {
|
||||||
|
delete m_bookmarkedItems[i];
|
||||||
|
m_bookmarkedItems.removeAt(i);
|
||||||
|
} else {
|
||||||
|
removeItem(modelIndex);
|
||||||
|
--modelIndex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (itemIsPartOfModel) {
|
||||||
|
++modelIndex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void PlacesItemModel::saveBookmarks()
|
void PlacesItemModel::saveBookmarks()
|
||||||
{
|
{
|
||||||
// TODO: Temporary deactivated until 100 % backward compatibility is provided
|
m_bookmarkManager->emitChanged(m_bookmarkManager->root());
|
||||||
// m_bookmarkManager->emitChanged(m_bookmarkManager->root());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlacesItemModel::loadBookmarks()
|
void PlacesItemModel::loadBookmarks()
|
||||||
|
@ -433,20 +535,13 @@ void PlacesItemModel::loadBookmarks()
|
||||||
QList<PlacesItem*> devicesItems;
|
QList<PlacesItem*> devicesItems;
|
||||||
|
|
||||||
while (!bookmark.isNull()) {
|
while (!bookmark.isNull()) {
|
||||||
const QString udi = bookmark.metaDataItem("UDI");
|
const bool deviceAvailable = devices.remove(bookmark.metaDataItem("UDI"));
|
||||||
const KUrl url = bookmark.url();
|
if (acceptBookmark(bookmark)) {
|
||||||
const QString appName = bookmark.metaDataItem("OnlyInApp");
|
|
||||||
const bool deviceAvailable = devices.remove(udi);
|
|
||||||
|
|
||||||
const bool allowedHere = (appName.isEmpty() || appName == KGlobal::mainComponent().componentName())
|
|
||||||
&& (m_nepomukRunning || (url.protocol() != QLatin1String("timeline") &&
|
|
||||||
url.protocol() != QLatin1String("search")));
|
|
||||||
|
|
||||||
if ((udi.isEmpty() && allowedHere) || deviceAvailable) {
|
|
||||||
PlacesItem* item = new PlacesItem(bookmark);
|
PlacesItem* item = new PlacesItem(bookmark);
|
||||||
if (deviceAvailable) {
|
if (deviceAvailable) {
|
||||||
devicesItems.append(item);
|
devicesItems.append(item);
|
||||||
} else {
|
} else {
|
||||||
|
const KUrl url = bookmark.url();
|
||||||
if (missingSystemBookmarks.contains(url)) {
|
if (missingSystemBookmarks.contains(url)) {
|
||||||
missingSystemBookmarks.remove(url);
|
missingSystemBookmarks.remove(url);
|
||||||
|
|
||||||
|
@ -483,8 +578,10 @@ void PlacesItemModel::loadBookmarks()
|
||||||
const QString protocol = data.url.protocol();
|
const QString protocol = data.url.protocol();
|
||||||
if (protocol == QLatin1String("timeline") || protocol == QLatin1String("search")) {
|
if (protocol == QLatin1String("timeline") || protocol == QLatin1String("search")) {
|
||||||
// As long as the KFilePlacesView from kdelibs is available, the system-bookmarks
|
// As long as the KFilePlacesView from kdelibs is available, the system-bookmarks
|
||||||
// for timeline and search should be a Dolphin-specific setting.
|
// for "Recently Accessed" and "Search For" should be a setting available only
|
||||||
bookmark.setMetaDataItem("OnlyInApp", KGlobal::mainComponent().componentName());
|
// in the Places Panel (see description of AppNamePrefix for more details).
|
||||||
|
const QString appName = KGlobal::mainComponent().componentName() + AppNamePrefix;
|
||||||
|
bookmark.setMetaDataItem("OnlyInApp", appName);
|
||||||
}
|
}
|
||||||
|
|
||||||
PlacesItem* item = new PlacesItem(bookmark);
|
PlacesItem* item = new PlacesItem(bookmark);
|
||||||
|
@ -501,17 +598,25 @@ void PlacesItemModel::loadBookmarks()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
addItems(placesItems);
|
// Create items for devices that have not been stored as bookmark yet
|
||||||
addItems(recentlyAccessedItems);
|
|
||||||
addItems(searchForItems);
|
|
||||||
|
|
||||||
// Create items for devices that have not stored as bookmark yet
|
|
||||||
foreach (const QString& udi, devices) {
|
foreach (const QString& udi, devices) {
|
||||||
const KBookmark bookmark = PlacesItem::createDeviceBookmark(m_bookmarkManager, udi);
|
const KBookmark bookmark = PlacesItem::createDeviceBookmark(m_bookmarkManager, udi);
|
||||||
devicesItems.append(new PlacesItem(bookmark));
|
devicesItems.append(new PlacesItem(bookmark));
|
||||||
}
|
}
|
||||||
|
|
||||||
addItems(devicesItems);
|
QList<PlacesItem*> items;
|
||||||
|
items.append(placesItems);
|
||||||
|
items.append(recentlyAccessedItems);
|
||||||
|
items.append(searchForItems);
|
||||||
|
items.append(devicesItems);
|
||||||
|
|
||||||
|
foreach (PlacesItem* item, items) {
|
||||||
|
if (!m_hiddenItemsShown && item->isHidden()) {
|
||||||
|
m_bookmarkedItems.append(item);
|
||||||
|
} else {
|
||||||
|
appendItem(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef PLACESITEMMODEL_DEBUG
|
#ifdef PLACESITEMMODEL_DEBUG
|
||||||
kDebug() << "Loaded bookmarks";
|
kDebug() << "Loaded bookmarks";
|
||||||
|
@ -519,15 +624,20 @@ void PlacesItemModel::loadBookmarks()
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlacesItemModel::addItems(const QList<PlacesItem*>& items)
|
bool PlacesItemModel::acceptBookmark(const KBookmark& bookmark) const
|
||||||
{
|
{
|
||||||
foreach (PlacesItem* item, items) {
|
const QString udi = bookmark.metaDataItem("UDI");
|
||||||
if (!m_hiddenItemsShown && item->isHidden()) {
|
const KUrl url = bookmark.url();
|
||||||
m_hiddenItems.append(item);
|
const QString appName = bookmark.metaDataItem("OnlyInApp");
|
||||||
} else {
|
const bool deviceAvailable = m_availableDevices.contains(udi);
|
||||||
appendItem(item);
|
|
||||||
}
|
const bool allowedHere = (appName.isEmpty()
|
||||||
}
|
|| appName == KGlobal::mainComponent().componentName()
|
||||||
|
|| appName == KGlobal::mainComponent().componentName() + AppNamePrefix)
|
||||||
|
&& (m_nepomukRunning || (url.protocol() != QLatin1String("timeline") &&
|
||||||
|
url.protocol() != QLatin1String("search")));
|
||||||
|
|
||||||
|
return (udi.isEmpty() && allowedHere) || deviceAvailable;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlacesItemModel::createSystemBookmarks()
|
void PlacesItemModel::createSystemBookmarks()
|
||||||
|
@ -605,28 +715,39 @@ void PlacesItemModel::initializeAvailableDevices()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int PlacesItemModel::hiddenIndex(int index) const
|
int PlacesItemModel::bookmarkIndex(int index) const
|
||||||
{
|
{
|
||||||
int hiddenIndex = 0;
|
int bookmarkIndex = 0;
|
||||||
int visibleItemIndex = 0;
|
int modelIndex = 0;
|
||||||
while (hiddenIndex < m_hiddenItems.count()) {
|
while (bookmarkIndex < m_bookmarkedItems.count()) {
|
||||||
if (!m_hiddenItems[hiddenIndex]) {
|
if (!m_bookmarkedItems[bookmarkIndex]) {
|
||||||
if (visibleItemIndex == index) {
|
if (modelIndex == index) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
++visibleItemIndex;
|
++modelIndex;
|
||||||
}
|
}
|
||||||
++hiddenIndex;
|
++bookmarkIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
return hiddenIndex >= m_hiddenItems.count() ? -1 : hiddenIndex;
|
return bookmarkIndex >= m_bookmarkedItems.count() ? -1 : bookmarkIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlacesItemModel::removeHiddenItem(int index)
|
void PlacesItemModel::hideItem(int index)
|
||||||
{
|
{
|
||||||
const PlacesItem* shownItem = placesItem(index);
|
PlacesItem* shownItem = placesItem(index);
|
||||||
const int newIndex = hiddenIndex(index);
|
if (!shownItem) {
|
||||||
if (shownItem && newIndex >= 0) {
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
shownItem->setHidden(true);
|
||||||
|
if (m_hiddenItemsShown) {
|
||||||
|
// Removing items from the model is not allowed if all hidden
|
||||||
|
// items should be shown.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int newIndex = bookmarkIndex(index);
|
||||||
|
if (newIndex >= 0) {
|
||||||
PlacesItem* hiddenItem = new PlacesItem(*shownItem);
|
PlacesItem* hiddenItem = new PlacesItem(*shownItem);
|
||||||
const KBookmark hiddenBookmark = hiddenItem->bookmark();
|
const KBookmark hiddenBookmark = hiddenItem->bookmark();
|
||||||
|
|
||||||
|
@ -636,36 +757,71 @@ void PlacesItemModel::removeHiddenItem(int index)
|
||||||
previousBookmark = previousItem->bookmark();
|
previousBookmark = previousItem->bookmark();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const bool updateBookmark = (m_bookmarkManager->root().indexOf(hiddenBookmark) >= 0);
|
||||||
removeItem(index);
|
removeItem(index);
|
||||||
|
|
||||||
// removeItem() also removed the bookmark from m_bookmarkManager in
|
if (updateBookmark) {
|
||||||
// PlacesItemModel::onItemRemoved(). However for hidden items the
|
// removeItem() also removed the bookmark from m_bookmarkManager in
|
||||||
// bookmark should still be remembered, so readd it again:
|
// PlacesItemModel::onItemRemoved(). However for hidden items the
|
||||||
m_bookmarkManager->root().addBookmark(hiddenBookmark);
|
// bookmark should still be remembered, so readd it again:
|
||||||
m_bookmarkManager->root().moveBookmark(hiddenBookmark, previousBookmark);
|
m_bookmarkManager->root().addBookmark(hiddenBookmark);
|
||||||
|
m_bookmarkManager->root().moveBookmark(hiddenBookmark, previousBookmark);
|
||||||
|
m_saveBookmarksTimer->start();
|
||||||
|
}
|
||||||
|
|
||||||
m_hiddenItems.insert(newIndex, hiddenItem);
|
m_bookmarkedItems.insert(newIndex, hiddenItem);
|
||||||
m_saveBookmarksTimer->start();
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PlacesItemModel::equalBookmarkIdentifiers(const KBookmark& b1, const KBookmark& b2)
|
||||||
|
{
|
||||||
|
const QString udi1 = b1.metaDataItem("UDI");
|
||||||
|
const QString udi2 = b2.metaDataItem("UDI");
|
||||||
|
if (!udi1.isEmpty() && !udi2.isEmpty()) {
|
||||||
|
return udi1 == udi2;
|
||||||
|
} else {
|
||||||
|
return b1.metaDataItem("ID") == b2.metaDataItem("ID");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef PLACESITEMMODEL_DEBUG
|
#ifdef PLACESITEMMODEL_DEBUG
|
||||||
void PlacesItemModel::showModelState()
|
void PlacesItemModel::showModelState()
|
||||||
{
|
{
|
||||||
kDebug() << "hidden-index model-index text";
|
kDebug() << "=================================";
|
||||||
int j = 0;
|
kDebug() << "Model:";
|
||||||
for (int i = 0; i < m_hiddenItems.count(); ++i) {
|
kDebug() << "hidden-index model-index text";
|
||||||
if (m_hiddenItems[i]) {
|
int modelIndex = 0;
|
||||||
kDebug() << i << "(Hidden) " << " " << m_hiddenItems[i]->dataValue("text").toString();
|
for (int i = 0; i < m_bookmarkedItems.count(); ++i) {
|
||||||
|
if (m_bookmarkedItems[i]) {
|
||||||
|
kDebug() << i << "(Hidden) " << " " << m_bookmarkedItems[i]->dataValue("text").toString();
|
||||||
} else {
|
} else {
|
||||||
if (item(j)) {
|
if (item(modelIndex)) {
|
||||||
kDebug() << i << " " << j << " " << item(j)->dataValue("text").toString();
|
kDebug() << i << " " << modelIndex << " " << item(modelIndex)->dataValue("text").toString();
|
||||||
} else {
|
} else {
|
||||||
kDebug() << i << " " << j << " " << "(not available yet)";
|
kDebug() << i << " " << modelIndex << " " << "(not available yet)";
|
||||||
}
|
}
|
||||||
++j;
|
++modelIndex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
kDebug();
|
||||||
|
kDebug() << "Bookmarks:";
|
||||||
|
|
||||||
|
int bookmarkIndex = 0;
|
||||||
|
KBookmarkGroup root = m_bookmarkManager->root();
|
||||||
|
KBookmark bookmark = root.first();
|
||||||
|
while (!bookmark.isNull()) {
|
||||||
|
const QString udi = bookmark.metaDataItem("UDI");
|
||||||
|
const QString text = udi.isEmpty() ? bookmark.text() : udi;
|
||||||
|
if (bookmark.metaDataItem("IsHidden") == QLatin1String("true")) {
|
||||||
|
kDebug() << bookmarkIndex << "(Hidden)" << text;
|
||||||
|
} else {
|
||||||
|
kDebug() << bookmarkIndex << " " << text;
|
||||||
|
}
|
||||||
|
|
||||||
|
bookmark = root.next(bookmark);
|
||||||
|
++bookmarkIndex;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -31,6 +31,7 @@
|
||||||
#include <Solid/Predicate>
|
#include <Solid/Predicate>
|
||||||
#include <Solid/StorageAccess>
|
#include <Solid/StorageAccess>
|
||||||
|
|
||||||
|
class KBookmark;
|
||||||
class KBookmarkManager;
|
class KBookmarkManager;
|
||||||
class PlacesItem;
|
class PlacesItem;
|
||||||
class QAction;
|
class QAction;
|
||||||
|
@ -52,15 +53,31 @@ public:
|
||||||
explicit PlacesItemModel(QObject* parent = 0);
|
explicit PlacesItemModel(QObject* parent = 0);
|
||||||
virtual ~PlacesItemModel();
|
virtual ~PlacesItemModel();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return A new instance of a places item with the given
|
||||||
|
* attributes.
|
||||||
|
*/
|
||||||
PlacesItem* createPlacesItem(const QString& text,
|
PlacesItem* createPlacesItem(const QString& text,
|
||||||
const KUrl& url,
|
const KUrl& url,
|
||||||
const QString& iconName);
|
const QString& iconName);
|
||||||
|
|
||||||
PlacesItem* placesItem(int index) const;
|
PlacesItem* placesItem(int index) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If set to true, all items that are marked as hidden
|
||||||
|
* will be shown in the view. The items will
|
||||||
|
* stay marked as hidden, which is visually indicated
|
||||||
|
* by the view by desaturating the icon and the text.
|
||||||
|
*/
|
||||||
void setHiddenItemsShown(bool show);
|
void setHiddenItemsShown(bool show);
|
||||||
bool hiddenItemsShown() const;
|
bool hiddenItemsShown() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Number of items that are marked as hidden.
|
||||||
|
* Note that this does not mean that the items
|
||||||
|
* are really hidden
|
||||||
|
* (see PlacesItemModel::setHiddenItemsShown()).
|
||||||
|
*/
|
||||||
int hiddenCount() const;
|
int hiddenCount() const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -90,18 +107,35 @@ private slots:
|
||||||
void slotDeviceAdded(const QString& udi);
|
void slotDeviceAdded(const QString& udi);
|
||||||
void slotDeviceRemoved(const QString& udi);
|
void slotDeviceRemoved(const QString& udi);
|
||||||
void slotStorageTeardownDone(Solid::ErrorType error, const QVariant& errorData);
|
void slotStorageTeardownDone(Solid::ErrorType error, const QVariant& errorData);
|
||||||
void removeHiddenItem();
|
void hideItem();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the bookmarks from the model corresponding to the changed
|
||||||
|
* bookmarks stored by the bookmark-manager. Is called whenever the bookmarks
|
||||||
|
* have been changed by another application.
|
||||||
|
*/
|
||||||
|
void updateBookmarks();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves the bookmarks and indicates to other applications that the
|
||||||
|
* state of the bookmarks has been changed. Is only called by the
|
||||||
|
* timeout of m_saveBookmarksTimer to prevent unnecessary savings.
|
||||||
|
*/
|
||||||
void saveBookmarks();
|
void saveBookmarks();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void loadBookmarks();
|
/**
|
||||||
|
* Loads the bookmarks from the bookmark-manager and creates items for
|
||||||
|
* the model or moves hidden items to m_bookmarkedItems.
|
||||||
|
*/
|
||||||
|
void loadBookmarks();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper method for loadBookmarks(): Adds the items
|
* @return True, if the bookmark can be accepted in the context of the
|
||||||
* to the model if the "isHidden"-property is false,
|
* current application (e.g. bookmarks from other applications
|
||||||
* otherwise the items get added to m_hiddenItems.
|
* will be ignored).
|
||||||
*/
|
*/
|
||||||
void addItems(const QList<PlacesItem*>& items);
|
bool acceptBookmark(const KBookmark& bookmark) const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates system bookmarks that are shown per default and can
|
* Creates system bookmarks that are shown per default and can
|
||||||
|
@ -114,11 +148,22 @@ private:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param index Item index related to the model.
|
* @param index Item index related to the model.
|
||||||
* @return Corresponding item index related to m_hiddenItems.
|
* @return Corresponding index related to m_bookmarkedItems.
|
||||||
*/
|
*/
|
||||||
int hiddenIndex(int index) const;
|
int bookmarkIndex(int index) const;
|
||||||
|
|
||||||
void removeHiddenItem(int index);
|
/**
|
||||||
|
* Marks the item with the index \a index as hidden and
|
||||||
|
* removes it from the model so that it gets invisible.
|
||||||
|
*/
|
||||||
|
void hideItem(int index);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return True if the bookmarks have the same identifiers. The identifier
|
||||||
|
* is the unique "ID"-property in case if no UDI is set, otherwise
|
||||||
|
* the UDI is used as identifier.
|
||||||
|
*/
|
||||||
|
static bool equalBookmarkIdentifiers(const KBookmark& b1, const KBookmark& b2);
|
||||||
|
|
||||||
#ifdef PLACESITEMMODEL_DEBUG
|
#ifdef PLACESITEMMODEL_DEBUG
|
||||||
void showModelState();
|
void showModelState();
|
||||||
|
@ -146,7 +191,12 @@ private:
|
||||||
QList<SystemBookmarkData> m_systemBookmarks;
|
QList<SystemBookmarkData> m_systemBookmarks;
|
||||||
QHash<KUrl, int> m_systemBookmarksIndexes;
|
QHash<KUrl, int> m_systemBookmarksIndexes;
|
||||||
|
|
||||||
QList<PlacesItem*> m_hiddenItems;
|
// Contains hidden and unhidden items that are stored as
|
||||||
|
// bookmark (the model itself only contains items that
|
||||||
|
// are shown in the view). If an entry is 0, then the
|
||||||
|
// places-item is part of the model. If an entry is not
|
||||||
|
// 0, the item is hidden and not part of the model.
|
||||||
|
QList<PlacesItem*> m_bookmarkedItems;
|
||||||
|
|
||||||
// Index of the hidden item that should be removed in
|
// Index of the hidden item that should be removed in
|
||||||
// removeHiddenItem(). The removing must be done
|
// removeHiddenItem(). The removing must be done
|
||||||
|
@ -155,6 +205,7 @@ private:
|
||||||
int m_hiddenItemToRemove;
|
int m_hiddenItemToRemove;
|
||||||
|
|
||||||
QTimer* m_saveBookmarksTimer;
|
QTimer* m_saveBookmarksTimer;
|
||||||
|
QTimer* m_updateBookmarksTimer;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue