bookmarkPlugin.cpp 3.8 KB
Newer Older
1
/***************************************************************************
2
 *   Copyright (C) 2004-2011 by Pere Constans
3
 *   constans@molspaces.com
4
 *   cb2Bib version 1.4.6. Licensed under the GNU GPL version 3.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *   See the LICENSE file that comes with this distribution.
 ***************************************************************************/
#include "bookmarkPlugin.h"

#include "c2b.h"
#include "c2bSettings.h"
#include "c2bUtils.h"

#include <QtGui>


bookmarkPlugin::bookmarkPlugin(QWidget* parentw) : QMenu(parentw)
{
    setObjectName(QString::fromUtf8("menuBookmarks"));
    setTitle(QApplication::translate("c2bEditor", "&Bookmarks", 0, QApplication::UnicodeUTF8));
    connect(c2bSettingsP, SIGNAL(newSettings()), this, SLOT(init()));
21 22
    _fsw = new QFileSystemWatcher(this);
    connect(_fsw, SIGNAL(fileChanged(const QString&)), this, SLOT(parseBookmarks()));
23 24 25 26 27 28 29 30 31
    init();
}

bookmarkPlugin::~bookmarkPlugin()
{}


void bookmarkPlugin::init()
{
32
    const QString nfn(c2bSettingsP->fileName("cb2Bib/NetworkFile"));
33
    if (nfn == _bookmark_file)
34 35 36
        return;
    if (QFileInfo(nfn).exists())
    {
37 38 39 40
        if (!_bookmark_file.isEmpty())
            _fsw->removePath(_bookmark_file);
        _bookmark_file = nfn;
        _fsw->addPath(_bookmark_file);
41 42 43 44 45 46 47
    }
    parseBookmarks();
}

void bookmarkPlugin::parseBookmarks()
{
    clear();
48 49
    const QString bmf(c2bUtils::fileToString(_bookmark_file));
    QStringList bookmarks(bmf.split('\n', QString::KeepEmptyParts));
50 51 52 53 54 55 56 57 58 59 60 61
    const QRegExp _bookmark("^editorbookmark=");
    const QRegExp _separator("^editorbookmark=\\s*$");
    for (int i = 0; i < bookmarks.count(); ++i)
    {
        if (bookmarks.at(i).contains(_separator))
        {
            addSeparator();
            continue;
        }
        if (bookmarks.at(i).contains(_bookmark))
        {
            bookmarks[i].remove(_bookmark);
62
            const QStringList bm_parts(bookmarks.at(i).split('|', QString::KeepEmptyParts));
63 64
            if (bm_parts.count() != 4)
            {
65
                c2bUtils::warn(tr("bookmarkPlugin::parseBookmarks: Syntax error at '%1'").arg(bookmarks.at(i)));
66 67 68 69 70 71 72 73 74 75 76
                continue;
            }
            QAction* act = addAction(bm_parts.at(0), this, SLOT(openBookmark()));
            act->setData(bm_parts.at(1));
            act->setStatusTip(QString("%1  [%2]").arg(bm_parts.at(0)).arg(bm_parts.at(1)));
            act->setShortcut(bm_parts.at(2));
            act->setIcon(QIcon(iconProvider(bm_parts.at(1), bm_parts.at(3))));
        }
    }
    addSeparator();
    QAction* edit_bookmarks = addAction(tr("Edit Bookmarks"), this, SIGNAL(editBookmarks()));
77
    edit_bookmarks->setStatusTip(tr("Edit bookmarks"));
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
    edit_bookmarks->setIcon(QIcon(":/icons/icons/edit.png"));
}

QString bookmarkPlugin::iconProvider(const QString& fn, const QString& iconFile)
{
    if (!iconFile.isEmpty())
        return iconFile;
    if (fn.contains(QRegExp("\\.(tex|dvi)\\b", Qt::CaseInsensitive)))
        return ":/icons/icons/application-x-tex.png";
    if (fn.contains(QRegExp("\\.(chm|djvu|pdf|ps)\\b", Qt::CaseInsensitive)))
        return ":/icons/icons/application-x-pdf.png";
    if (fn.contains(QRegExp("\\.(htm|html|php)\\b", Qt::CaseInsensitive)))
        return ":/icons/icons/application-x-html.png";
    if (fn.contains(QRegExp("\\.txt\\b", Qt::CaseInsensitive)))
        return ":/icons/icons/application-x-tex.png";
    if (fn.contains(QRegExp("\\.bib\\b", Qt::CaseInsensitive)))
        return ":/icons/icons/cb2bib.png";
    return ":/icons/icons/application-x-none.png";
}

void bookmarkPlugin::openBookmark()
{
    QAction* act = static_cast<QAction*>(sender());
    if (act)
    {
103
        const QString fn(act->data().toString());
104 105 106 107 108 109
        if (fn.endsWith(".bib"))  // Open BiBTeX files internally
            emit openFile(fn);
        else
            c2bUtils::openFile(fn, parentWidget());
    }
}