File: c2bNetworkQueryInfo.cpp

package info (click to toggle)
cb2bib 1.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,368 kB
  • sloc: cpp: 24,179; sh: 481; makefile: 16
file content (195 lines) | stat: -rw-r--r-- 6,607 bytes parent folder | download
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
/***************************************************************************
 *   Copyright (C) 2004-2015 by Pere Constans
 *   constans@molspaces.com
 *   cb2Bib version 1.9.2. Licensed under the GNU GPL version 3.
 *   See the LICENSE file that comes with this distribution.
 ***************************************************************************/
#include "c2bNetworkQueryInfo.h"

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

#include <QAction>
#include <QFileSystemWatcher>
#include <QTextStream>
#include <QUrl>


c2bNetworkQueryInfo::c2bNetworkQueryInfo(QObject* parento) : QObject(parento)
{
    _bpP = c2b::instance()->bibParser();
    connect(c2bSettingsP, SIGNAL(newSettings()), this, SLOT(loadSettings()));
    _fsw = new QFileSystemWatcher(this);
    connect(_fsw, SIGNAL(fileChanged(const QString&)), this, SLOT(parseBookmarks()));
    loadSettings();
}

c2bNetworkQueryInfo::~c2bNetworkQueryInfo()
{}


void c2bNetworkQueryInfo::loadSettings()
{
    const QString nfn(c2bSettingsP->fileName("cb2Bib/NetworkFile"));
    if (nfn == NetworkFile)
        return;
    if (QFileInfo(nfn).exists())
    {
        if (!NetworkFile.isEmpty())
            _fsw->removePath(NetworkFile);
        NetworkFile = nfn;
        _fsw->addPath(NetworkFile);
    }
    parseBookmarks();
}

void c2bNetworkQueryInfo::parseBookmarks()
{
    BookmarkList.clear();
    if (NetworkFile.isEmpty())
        return;
    QFile file(NetworkFile);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;
    QRegExp _bookmark("^bookmark=");
    QString line;
    QTextStream stream(&file);
    stream.setCodec("UTF-8");
    stream.setAutoDetectUnicode(true);
    while (!stream.atEnd())
    {
        line = stream.readLine();
        if (line.contains(_bookmark))
        {
            line.remove(_bookmark);
            if (line.count('|') == 0)
                c2bUtils::warn(tr("c2bNetworkQueryInfo::parseBookmarks: Syntax error at '%1'").arg(line));
            else
                BookmarkList.append(line);
        }
    }
    file.close();
    while (!BookmarkActionList.isEmpty())
        delete BookmarkActionList.takeFirst();
    const QRegExp rx("<<.+>>");
    for (int i = 0; i < BookmarkList.count(); ++i)
        if (BookmarkList.at(i).contains(rx))
        {
            const QStringList bm_parts(BookmarkList.at(i).split('|'));
            if (bm_parts.count() > 1)
            {
                QAction* act = new QAction(bm_parts.at(0), this);
                act->setData(bm_parts.at(1));
                act->setStatusTip(QString("%1  [%2]").arg(bm_parts.at(0)).arg(bm_parts.at(1)));
                connect(act, SIGNAL(triggered()), this, SLOT(openBookmark()));
                BookmarkActionList.append(act);
            }
        }
}

QList<QAction*> c2bNetworkQueryInfo::bookmarkActions(const QString& selection)
{
    _current_selection = selection;
    for (int i = 0; i < BookmarkActionList.count(); ++i)
        BookmarkActionList[i]->setVisible(false);
    const QRegExp rx("<<.+>>");
    for (int i = 0; i < BookmarkActionList.count(); ++i)
    {
        QString bmd(BookmarkActionList.at(i)->data().toString());
        const QStringList& bibliographicFields = _bpP->bibliographicFields();
        for (int j = 0; j < bibliographicFields.count(); ++j)
        {
            const QString& name = bibliographicFields.at(j);
            const QString tag(QString("<<%1>>").arg(name));
            if (bmd.contains(tag))
                if (!_bpP->field(name).isEmpty())
                    bmd.remove(tag);
        }
        if (bmd.contains("<<selection>>"))
            if (!_current_selection.isEmpty())
                bmd.remove("<<selection>>");
        BookmarkActionList[i]->setVisible(!bmd.contains(rx));
    }
    return BookmarkActionList;
}

void c2bNetworkQueryInfo::openBookmark()
{
    QAction* act = static_cast<QAction*>(sender());
    if (act)
    {
        const QString link(act->data().toString());
        c2bUtils::openFile(encodeLink(link), c2b::mainWidget());
    }
}

const QString c2bNetworkQueryInfo::encodeLink(const QString& ln) const
{
    QString eln(ln);
    // See comment in c2bNetworkQueryInfo::bookmarksToHtml
    eln.replace("[[[", "<<");
    eln.replace("]]]", ">>");
    const QRegExp rx("<<.+>>");
    if (eln.contains(rx))
    {
        const QStringList& bibliographicFields = _bpP->bibliographicFields();
        for (int j = 0; j < bibliographicFields.count(); ++j)
        {
            const QString& name = bibliographicFields.at(j);
            const QString tag(QString("<<%1>>").arg(name));
            if (eln.contains(tag))
            {
                QString fld;
                if (name == "pages")
                    fld = _bpP->field(name).replace(QRegExp("-.+$"), QString());
                else if (name == "author")
                {
                    fld = _bpP->field(name);
                    fld.replace(" and ", " ");
                }
                else if (name == "title")
                {
                    fld = _bpP->field(name);
                    fld = fld.remove('{');
                    fld = fld.remove('}');
                }
                else
                    fld = _bpP->field(name);
                fld = QUrl::toPercentEncoding(fld);
                eln.replace(tag, fld);
                if (!eln.contains(rx))
                    return eln;
            }
        }
        eln.replace("<<selection>>", _current_selection);
    }
    return eln;
}

const QString c2bNetworkQueryInfo::bookmarksToHtml() const
{
    if (BookmarkList.count() == 0)
        return QString();
    QString html("<p align=\"center\">Bookmarks:<br /><p align=\"center\"><em>");
    for (int i = 0; i < BookmarkList.count(); ++i)
    {
        int separator(BookmarkList.at(i).indexOf('|'));
        if (separator == -1)
            continue;
        QString url_name(BookmarkList.at(i).mid(0, separator));
        QString url(BookmarkList.at(i).mid(separator + 1));
        // Tags <<.>> conflict with the Qt > 4.4.x HTML parser, even being them inside a
        // literal, quoted string. The substitution below works as a workaround to prevent
        // cb2Bib tags from clashing with the parser.
        // See c2bNetworkQueryInfo::encodeLink. A similar issue was resolved in
        // fromQtXmlString/toQtXmlString.
        url.replace("<<", "[[[");
        url.replace(">>", "]]]");
        url = "<a href=\"" + url + "\">" + url_name + "</a>";
        html += url + " <br />";
    }
    html += "</em></p><br />";
    return html;
}