OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / help / remotehelpfilter.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "remotehelpfilter.h"
34
35 #include <QtCore/QUrl>
36
37 namespace Help {
38     namespace Internal {
39
40 RemoteFilterOptions::RemoteFilterOptions(RemoteHelpFilter *filter, QWidget *parent)
41     : QDialog(parent)
42     , m_filter(filter)
43 {
44     m_ui.setupUi(this);
45     m_ui.shortcutEdit->setText(m_filter->shortcutString());
46     m_ui.limitCheck->setChecked(!m_filter->isIncludedByDefault());
47     foreach (const QString &url, m_filter->remoteUrls()) {
48         QListWidgetItem *item = new QListWidgetItem(url);
49         m_ui.listWidget->addItem(item);
50         item->setFlags(item->flags() | Qt::ItemIsEditable);
51     }
52
53     connect(m_ui.add, SIGNAL(clicked()), this, SLOT(addNewItem()));
54     connect(m_ui.remove, SIGNAL(clicked()), this, SLOT(removeItem()));
55 }
56
57 void RemoteFilterOptions::addNewItem()
58 {
59     QListWidgetItem *item = new QListWidgetItem(tr("Double-click to edit item."));
60     m_ui.listWidget->addItem(item);
61     item->setSelected(true);
62     item->setFlags(item->flags() | Qt::ItemIsEditable);
63     m_ui.listWidget->editItem(item);
64 }
65
66 void RemoteFilterOptions::removeItem()
67 {
68     if (QListWidgetItem *item = m_ui.listWidget->currentItem()) {
69         m_ui.listWidget->removeItemWidget(item);
70         delete item;
71     }
72 }
73
74 // -- RemoteHelpFilter
75
76 RemoteHelpFilter::RemoteHelpFilter()
77 {
78     setIncludedByDefault(false);
79     setShortcutString(QLatin1String("r"));
80     m_remoteUrls.append(QLatin1String("http://www.bing.com/search?q=%1"));
81     m_remoteUrls.append(QLatin1String("http://www.google.com/search?q=%1"));
82     m_remoteUrls.append(QLatin1String("http://search.yahoo.com/search?p=%1"));
83     m_remoteUrls.append(QLatin1String("http://www.cplusplus.com/reference/stl/%1"));
84     m_remoteUrls.append(QLatin1String("http://en.wikipedia.org/w/index.php?search=%1"));
85 }
86
87 RemoteHelpFilter::~RemoteHelpFilter()
88 {
89 }
90
91 QString RemoteHelpFilter::displayName() const
92 {
93     return tr("Web Search");
94 }
95
96 QString RemoteHelpFilter::id() const
97 {
98     return QLatin1String("RemoteHelpFilter");
99 }
100
101 Locator::ILocatorFilter::Priority RemoteHelpFilter::priority() const
102 {
103     return Medium;
104 }
105
106 QList<Locator::FilterEntry> RemoteHelpFilter::matchesFor(QFutureInterface<Locator::FilterEntry> &future, const QString &pattern)
107 {
108     QList<Locator::FilterEntry> entries;
109     foreach (const QString &url, m_remoteUrls) {
110         if (future.isCanceled())
111             break;
112
113         entries.append(Locator::FilterEntry(this, url.arg(pattern), QVariant(),
114             m_icon));
115     }
116     return entries;
117 }
118
119 void RemoteHelpFilter::accept(Locator::FilterEntry selection) const
120 {
121     const QString &url = selection.displayName;
122     if (!url.isEmpty()) {
123         emit linkActivated(url);
124     }
125 }
126
127 void RemoteHelpFilter::refresh(QFutureInterface<void> &future)
128 {
129     Q_UNUSED(future)
130     // Nothing to refresh
131 }
132
133 QByteArray RemoteHelpFilter::saveState() const
134 {
135     QByteArray value;
136     QDataStream out(&value, QIODevice::WriteOnly);
137     out << m_remoteUrls.join(QLatin1String("^"));
138     out << shortcutString();
139     out << isIncludedByDefault();
140     return value;
141 }
142
143 bool RemoteHelpFilter::restoreState(const QByteArray &state)
144 {
145     QDataStream in(state);
146
147     QString value;
148     in >> value;
149     m_remoteUrls = value.split(QLatin1String("^"), QString::SkipEmptyParts);
150
151     QString shortcut;
152     in >> shortcut;
153     setShortcutString(shortcut);
154
155     bool defaultFilter;
156     in >> defaultFilter;
157     setIncludedByDefault(defaultFilter);
158
159     return true;
160 }
161
162 bool RemoteHelpFilter::openConfigDialog(QWidget *parent, bool &needsRefresh)
163 {
164     Q_UNUSED(needsRefresh)
165     RemoteFilterOptions optionsDialog(this, parent);
166     if (optionsDialog.exec() == QDialog::Accepted) {
167         m_remoteUrls.clear();
168         setIncludedByDefault(!optionsDialog.m_ui.limitCheck->isChecked());
169         setShortcutString(optionsDialog.m_ui.shortcutEdit->text().trimmed());
170         for (int i = 0; i < optionsDialog.m_ui.listWidget->count(); ++i)
171             m_remoteUrls.append(optionsDialog.m_ui.listWidget->item(i)->text());
172         return true;
173     }
174     return true;
175 }
176
177     } // namespace Internal
178 } // namespace Help