/* ============================================================ * * This file is a part of the rekonq project * * Copyright (C) 2010-2011 by Yoann Laissus * Copyright (C) 2012 by Andrea Diamantini * * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * ============================================================ */ // Auto Includes #include "bookmarkwidget.h" #include "bookmarkwidget.moc" // Local includes #include "application.h" #include "bookmarkmanager.h" #include "bookmarkowner.h" // KDE Includes #include #include #include #include #include // Qt Includes #include #include #include #include #include #include // Nepomuk config include #include "../config-nepomuk.h" #ifdef HAVE_NEPOMUK // Local Nepomuk Includes #include "resourcelinkdialog.h" //Nepomuk Includes #include #endif BookmarkWidget::BookmarkWidget(const KBookmark &bookmark, QWidget *parent) : QMenu(parent) , m_bookmark(new KBookmark(bookmark)) { setAttribute(Qt::WA_DeleteOnClose); setFixedWidth(320); #ifdef HAVE_NEPOMUK m_nfoResource = (QUrl)m_bookmark->url(); #endif QFormLayout *layout = new QFormLayout(this); layout->setHorizontalSpacing(20); // Title QHBoxLayout *hLayout = new QHBoxLayout; QLabel *bookmarkInfo = new QLabel(this); bookmarkInfo->setText(i18n(" Bookmark")); QFont f = bookmarkInfo->font(); f.setBold(true); bookmarkInfo->setFont(f); // Remove button QLabel *removeLabel = new QLabel( this ); removeLabel->setText( i18n( "Remove" ) ); removeLabel->setAlignment( Qt::AlignRight ); hLayout->addWidget(bookmarkInfo); hLayout->addWidget(removeLabel); layout->addRow(hLayout); connect(removeLabel, SIGNAL( linkActivated(QString) ), this, SLOT( removeBookmark() )); //Bookmark Folder QLabel *folderLabel = new QLabel(this); folderLabel->setText(i18n("Folder:")); m_folder = new KComboBox(this); layout->addRow(folderLabel, m_folder); setupFolderComboBox(); // Bookmark name QLabel *nameLabel = new QLabel(this); nameLabel->setText(i18n("Name:")); m_name = new KLineEdit(this); if (m_bookmark->isNull()) { m_name->setEnabled(false); } else { m_name->setText(m_bookmark->text()); m_name->setFocus(); } layout->addRow(nameLabel, m_name); #ifdef HAVE_NEPOMUK QLabel* rateLabel = new QLabel(this); rateLabel->setText( i18n( "Rate:" ) ); KRatingWidget *ratingWidget = new KRatingWidget( this ); if( m_nfoResource.rating() != NULL ) { ratingWidget->setRating( m_nfoResource.rating() ); } connect( ratingWidget, SIGNAL( ratingChanged( int ) ), this, SLOT( setRatingSlot( int ) ) ); ratingWidget->setToolTip( i18n( "Rate this page" ) ); layout->addRow( rateLabel,ratingWidget ); //Add comments QLabel *commentLabel = new QLabel( this ); commentLabel->setText( i18n( "Describe:" ) ); commentLabel->setAlignment( Qt::AlignCenter ); m_commentEdit = new QPlainTextEdit( this ); if( !m_nfoResource.description().isEmpty() ) { m_commentEdit->setPlainText( m_nfoResource.description() ); } connect( m_commentEdit, SIGNAL(textChanged()), this, SLOT(addCommentSlot()) ); layout->addRow( commentLabel, m_commentEdit ); // Create tags QLabel *tagLabel = new QLabel( this ); tagLabel->setText( i18n( "Tags:" ) ); tagLabel->setAlignment( Qt::AlignLeft ); m_tagLine = new KLineEdit( this ); m_tagLine->setPlaceholderText( i18n( "add tags(comma separated)" ) ); QList tagList = Nepomuk::Tag::allTags(); Q_FOREACH(Nepomuk::Tag t,tagList) { m_tList.append(t.label()); } QCompleter *completeTag = new QCompleter( m_tList ); completeTag->setCompletionMode(QCompleter::PopupCompletion); m_tagLine->setCompleter(completeTag); loadTags(); layout->addRow(tagLabel,m_tagLine); QPushButton *linkToResource = new QPushButton( this ); linkToResource->setText( i18n( "Link Resources" ) ); connect(linkToResource, SIGNAL(clicked()), this, SLOT( linkToResourceSlot() ) ); layout->addWidget(linkToResource); #endif // Ok & Cancel buttons QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this); connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())); connect(buttonBox, SIGNAL(rejected()), this, SLOT(close())); layout->addWidget(buttonBox); } BookmarkWidget::~BookmarkWidget() { delete m_bookmark; } void BookmarkWidget::showAt(const QPoint &pos) { adjustSize(); QPoint p(pos.x() - width(), pos.y() + 10); move(p); show(); } void BookmarkWidget::accept() { if (!m_bookmark->isNull() && m_name->text() != m_bookmark->fullText()) { m_bookmark->setFullText(m_name->text()); rApp->bookmarkManager()->emitChanged(); } QString folderAddress = m_folder->itemData(m_folder->currentIndex()).toString(); KBookmarkGroup a = rApp->bookmarkManager()->manager()->findByAddress(folderAddress).toGroup(); KBookmarkGroup parent = m_bookmark->parentGroup(); parent.deleteBookmark(*m_bookmark); a.addBookmark(*m_bookmark); rApp->bookmarkManager()->manager()->emitChanged(a); #ifdef HAVE_NEPOMUK parseTags(); #endif close(); } void BookmarkWidget::setupFolderComboBox() { KBookmarkGroup root = rApp->bookmarkManager()->manager()->toolbar(); if (rApp->bookmarkManager()->manager()->toolbar().address() == rApp->bookmarkManager()->manager()->root().address()) { m_folder->addItem(i18n("Bookmark Toolbar"), rApp->bookmarkManager()->manager()->toolbar().address()); } else { m_folder->addItem(rApp->bookmarkManager()->manager()->toolbar().text(), rApp->bookmarkManager()->manager()->toolbar().address()); } m_folder->insertSeparator(1); if (m_bookmark->parentGroup().address() != rApp->bookmarkManager()->manager()->toolbar().address()) { m_folder->addItem(m_bookmark->parentGroup().text(), m_bookmark->parentGroup().address()); m_folder->insertSeparator(3); } for (KBookmark bookmark = root.first(); !bookmark.isNull(); bookmark = root.next(bookmark)) { if (bookmark.isGroup()) { m_folder->addItem(bookmark.text(), bookmark.address()); } } if (m_bookmark->parentGroup().address() == root.address()) { m_folder->setCurrentIndex(0); } else { int index = m_folder->findText(m_bookmark->parentGroup().text()); m_folder->setCurrentIndex(index); } } void BookmarkWidget::removeBookmark() { rApp->bookmarkManager()->owner()->deleteBookmark(*m_bookmark); close(); emit updateIcon(); } #ifdef HAVE_NEPOMUK void BookmarkWidget::addTags(QList tagList) { foreach ( const Nepomuk::Tag &tag,tagList) { if (!m_nfoResource.tags().contains(tag)) { m_nfoResource.addTag(tag); } } foreach ( Nepomuk::Tag tag,m_nfoResource.tags()) { if (!tagList.contains(tag)) { tag.remove(); } } } void BookmarkWidget::parseTags() { QList tagList; if(m_tagLine->text().contains(',')) { QString text = m_tagLine->text(); QStringList tagStringList = text.split( QChar::fromAscii(',') ); foreach( const QString &tag, tagStringList ) { QString trimmedTag = tag.trimmed(); if (!trimmedTag.isEmpty()) tagList << trimmedTag; } } else { tagList << m_tagLine->text().trimmed(); } addTags(tagList); } void BookmarkWidget::loadTags() { QString list; if(!m_nfoResource.tags().isEmpty()) { foreach( const Nepomuk::Tag &tag, m_nfoResource.tags() ) { list.append(tag.genericLabel()); list.append(","); } m_tagLine->setText(list); } } void BookmarkWidget::setRatingSlot( int rate ) { m_nfoResource.setRating(rate); } void BookmarkWidget::addCommentSlot() { m_nfoResource.setDescription(m_commentEdit->toPlainText()); } void BookmarkWidget::linkToResourceSlot() { Nepomuk::ResourceLinkDialog r( m_nfoResource ); r.exec(); } #endif