OSDN Git Service

059bc604ddb379af9c7bc70b71878ea48ff0958d
[qt-creator-jp/qt-creator-jp.git] / src / plugins / git / gitorious / gitorioushostwidget.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 (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "gitorious.h"
35 #include "gitorioushostwidget.h"
36 #include "ui_gitorioushostwidget.h"
37
38 #include <coreplugin/coreconstants.h>
39
40 #include <QtCore/QUrl>
41 #include <QtCore/QDebug>
42 #include <QtCore/QTimer>
43
44 #include <QtGui/QStandardItem>
45 #include <QtGui/QStandardItemModel>
46 #include <QtGui/QItemSelectionModel>
47 #include <QtGui/QDesktopServices>
48 #include <QtGui/QIcon>
49 #include <QtGui/QStyle>
50
51 enum { debug = 0 };
52
53 namespace Gitorious {
54 namespace Internal {
55
56 enum { HostNameColumn, ProjectCountColumn, DescriptionColumn, ColumnCount };
57
58 // Create a model row for a host. Make the host name editable as specified by
59 // flag.
60 static QList<QStandardItem *> hostEntry(const QString &host,
61                                         int projectCount,
62                                         const QString &description, bool isDummyEntry)
63 {
64     const Qt::ItemFlags nonEditableFlags = (Qt::ItemIsSelectable|Qt::ItemIsEnabled);
65     const Qt::ItemFlags editableFlags = nonEditableFlags|Qt::ItemIsEditable;
66     QStandardItem *hostItem = new QStandardItem(host);
67     hostItem->setFlags(isDummyEntry ? editableFlags : nonEditableFlags);
68     // Empty for dummy, else "..." or count
69     QStandardItem *projectCountItem = 0;
70     QString countItemText;
71     if (!isDummyEntry) {
72         countItemText = projectCount ? QString::number(projectCount) : QString(QLatin1String("..."));
73     }
74     projectCountItem = new QStandardItem(countItemText);
75     projectCountItem->setFlags(nonEditableFlags);
76     QStandardItem *descriptionItem = new QStandardItem(description);
77     descriptionItem->setFlags(editableFlags);
78     QList<QStandardItem *> rc;
79     rc << hostItem << projectCountItem << descriptionItem;
80     return rc;
81 }
82
83 static inline QList<QStandardItem *> hostEntry(const GitoriousHost &h)
84 {
85     return hostEntry(h.hostName, h.projects.size(), h.description, false);
86 }
87
88 GitoriousHostWidget::GitoriousHostWidget(QWidget *parent) :
89     QWidget(parent),
90     m_newHost(tr("<New Host>")),
91     ui(new Ui::GitoriousHostWidget),
92     m_model(new QStandardItemModel(0, ColumnCount)),
93     m_errorClearTimer(0),
94     m_isValid(false),
95     m_isHostListDirty(false)
96 {
97     ui->setupUi(this);
98     ui->errorLabel->setVisible(false);
99     ui->browseToolButton->setIcon(style()->standardIcon(QStyle::SP_MessageBoxInformation));
100     connect(ui->browseToolButton, SIGNAL(clicked()), this, SLOT(slotBrowse()));
101     ui->browseToolButton->setEnabled(false);
102     ui->deleteToolButton->setIcon(QIcon(Core::Constants::ICON_MINUS));
103     connect(ui->deleteToolButton, SIGNAL(clicked()), this, SLOT(slotDelete()));
104     ui->deleteToolButton->setEnabled(false);
105
106     // Model
107     QStringList headers;
108     headers << tr("Host") << tr("Projects") << tr("Description");
109     m_model->setHorizontalHeaderLabels(headers);
110
111     Gitorious &gitorious = Gitorious::instance();
112     foreach( const GitoriousHost &gh, gitorious.hosts())
113         m_model->appendRow(hostEntry(gh));
114     appendNewDummyEntry();
115     connect(m_model, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(slotItemEdited(QStandardItem*)));
116     ui->hostView->setModel(m_model);
117
118     // View
119     ui->hostView->setRootIsDecorated(false);
120     ui->hostView->setUniformRowHeights(true);
121     connect(ui->hostView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
122             this, SLOT(slotCurrentChanged(QModelIndex,QModelIndex)));
123
124     ui->hostView->setSelectionMode(QAbstractItemView::SingleSelection);
125     if (m_model->rowCount())
126         selectRow(0);
127
128     connect(&gitorious, SIGNAL(projectListPageReceived(int,int)),
129             this, SLOT(slotProjectListPageReceived(int)));
130     connect(&gitorious, SIGNAL(projectListReceived(int)),
131             this, SLOT(slotProjectListPageReceived(int)));
132
133     connect(&gitorious, SIGNAL(error(QString)), this, SLOT(slotError(QString)));
134
135     setMinimumWidth(700);
136 }
137
138 GitoriousHostWidget::~GitoriousHostWidget()
139 {
140     // Prevent crash?
141     Gitorious *gitorious = &Gitorious::instance();
142     disconnect(gitorious, SIGNAL(projectListPageReceived(int,int)),
143                this, SLOT(slotProjectListPageReceived(int)));
144     disconnect(gitorious, SIGNAL(projectListReceived(int)),
145                this, SLOT(slotProjectListPageReceived(int)));
146     disconnect(gitorious, SIGNAL(error(QString)), this, SLOT(slotError(QString)));
147     delete ui;
148 }
149
150 int GitoriousHostWidget::selectedRow() const
151 {
152     const QModelIndex idx = ui->hostView->selectionModel()->currentIndex();
153     if (idx.isValid())
154         return idx.row();
155     return -1;
156 }
157
158 void GitoriousHostWidget::selectRow(int r)
159 {
160     if (r >= 0 && r != selectedRow()) {
161         const QModelIndex index = m_model->index(r, 0);
162         ui->hostView->selectionModel()->setCurrentIndex(index, QItemSelectionModel::Select|QItemSelectionModel::Current|QItemSelectionModel::Rows);
163     }
164 }
165
166 void GitoriousHostWidget::appendNewDummyEntry()
167 {
168     // Append a new entry where a host name is editable
169     m_model->appendRow(hostEntry(m_newHost, 0, QString(), true));
170 }
171
172 void GitoriousHostWidget::slotItemEdited(QStandardItem *item)
173 {
174     // Synchronize with Gitorious singleton.
175     // Did someone enter a valid host name into the dummy item?
176     // -> Create a new one.
177     const int row = item->row();
178     const bool isDummyEntry = row >= Gitorious::instance().hostCount();
179     switch (item->column()) {
180     case HostNameColumn:
181         if (isDummyEntry) {
182             Gitorious::instance().addHost(item->text(), m_model->item(row, DescriptionColumn)->text());
183             m_isHostListDirty = true;
184             appendNewDummyEntry();
185             selectRow(row);
186         }
187         break;
188     case ProjectCountColumn:
189         break;
190     case DescriptionColumn:
191         if (!isDummyEntry) {
192             const QString description = item->text();
193             if (description != Gitorious::instance().hostDescription(row)) {
194                 Gitorious::instance().setHostDescription(row, item->text());
195                 m_isHostListDirty = true;
196             }
197         }
198         break;
199     }
200 }
201
202 void GitoriousHostWidget::slotProjectListPageReceived(int row)
203 {
204     if (debug)
205         qDebug() << Q_FUNC_INFO << row;
206     // Update column
207     const int projectCount = Gitorious::instance().projectCount(row);
208     m_model->item(row, ProjectCountColumn)->setText(QString::number(projectCount));
209     // If it is the currently selected host, re-check validity if not enabled
210     if (!m_isValid) {
211         const QModelIndex current = ui->hostView->selectionModel()->currentIndex();
212         if (current.isValid() && current.row() == row)
213             checkValid(current);
214     }
215 }
216
217 QStandardItem *GitoriousHostWidget::currentItem() const
218 {
219     const QModelIndex idx = ui->hostView->selectionModel()->currentIndex();
220     if (idx.isValid())
221         return m_model->itemFromIndex(idx.column() != 0 ? idx.sibling(idx.row(), 0) : idx);
222     return 0;
223 }
224
225 void GitoriousHostWidget::slotBrowse()
226 {
227     if (const QStandardItem *item = currentItem()) {
228         const QUrl url(QLatin1String("http://") + item->text() + QLatin1Char('/'));
229         if (url.isValid())
230             QDesktopServices::openUrl(url);
231     }
232 }
233
234 void GitoriousHostWidget::slotDelete()
235 {
236     const QModelIndex index = ui->hostView->selectionModel()->currentIndex();
237     ui->hostView->selectionModel()->clear();
238     if (index.isValid()) {
239         const int row = index.row();
240         qDeleteAll(m_model->takeRow(row));
241         Gitorious::instance().removeAt(row);
242         m_isHostListDirty = true;
243     }
244 }
245
246 void GitoriousHostWidget::slotCurrentChanged(const QModelIndex &current, const QModelIndex & /* previous */)
247 {
248     checkValid(current);
249 }
250
251 void GitoriousHostWidget::checkValid(const QModelIndex &index)
252 {
253     if (debug)
254         qDebug() << Q_FUNC_INFO << index;
255     bool hasSelectedHost = false;
256     bool hasProjects = false;
257     if (index.isValid()) {
258         // Are we on the new dummy item?
259         Gitorious &gitorious = Gitorious::instance();
260         const int row = index.row();
261         hasSelectedHost = row < gitorious.hostCount();
262         hasProjects = hasSelectedHost && gitorious.projectCount(row) > 0;
263     }
264     ui->deleteToolButton->setEnabled(hasSelectedHost);
265     ui->browseToolButton->setEnabled(hasSelectedHost);
266
267     const bool valid = hasSelectedHost && hasProjects;
268     if (valid != m_isValid) {
269         m_isValid = valid;
270         emit validChanged();
271     }
272 }
273
274 bool GitoriousHostWidget::isValid() const
275 {
276     return m_isValid;
277 }
278
279 bool GitoriousHostWidget::isHostListDirty() const
280 {
281     return m_isHostListDirty;
282 }
283
284 void GitoriousHostWidget::slotClearError()
285 {
286     ui->errorLabel->setVisible(false);
287     ui->errorLabel->clear();
288 }
289
290 void GitoriousHostWidget::slotError(const QString &e)
291 {
292     // Display error for a while
293     ui->errorLabel->setText(e);
294     ui->errorLabel->setVisible(true);
295     if (!m_errorClearTimer) {
296         m_errorClearTimer = new QTimer(this);
297         m_errorClearTimer->setSingleShot(true);
298         m_errorClearTimer->setInterval(5000);
299         connect(m_errorClearTimer, SIGNAL(timeout()), this, SLOT(slotClearError()));
300     }
301     if (!m_errorClearTimer->isActive())
302         m_errorClearTimer->start();
303 }
304
305 void GitoriousHostWidget::changeEvent(QEvent *e)
306 {
307     QWidget::changeEvent(e);
308     switch (e->type()) {
309     case QEvent::LanguageChange:
310         ui->retranslateUi(this);
311         break;
312     default:
313         break;
314     }
315 }
316
317 } // namespace Internal
318 } // namespace Gitorious