OSDN Git Service

Git: Support encoding convert of commit messages
[qt-creator-jp/qt-creator-jp.git] / src / plugins / git / gitsubmiteditor.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 info@qt.nokia.com.
30 **
31 **************************************************************************/
32
33 #include "gitsubmiteditor.h"
34 #include "gitsubmiteditorwidget.h"
35 #include "gitconstants.h"
36 #include "commitdata.h"
37
38 #include <vcsbase/submitfilemodel.h>
39
40 #include <QtCore/QDebug>
41 #include <QtCore/QStringList>
42 #include <QtCore/QTextCodec>
43
44 namespace Git {
45 namespace Internal {
46
47 enum { FileTypeRole = Qt::UserRole + 1 };
48 enum FileType { StagedFile , UnstagedFile, UntrackedFile };
49
50 /* The problem with git is that no diff can be obtained to for a random
51  * multiselection of staged/unstaged files; it requires the --cached
52  * option for staged files. So, we sort apart the diff file lists
53  * according to a type flag we add to the model. */
54
55 GitSubmitEditor::GitSubmitEditor(const VCSBase::VCSBaseSubmitEditorParameters *parameters, QWidget *parent) :
56     VCSBaseSubmitEditor(parameters, new GitSubmitEditorWidget(parent)),
57     m_model(0)
58 {
59     connect(this, SIGNAL(diffSelectedFiles(QStringList)), this, SLOT(slotDiffSelected(QStringList)));
60 }
61
62 GitSubmitEditorWidget *GitSubmitEditor::submitEditorWidget()
63 {
64     return static_cast<GitSubmitEditorWidget *>(widget());
65 }
66
67 // Utility to add a list of state/file pairs to the model
68 // setting a file type.
69 static void addStateFileListToModel(const QList<CommitData::StateFilePair> &l,
70                                     bool checked, FileType ft,
71                                     VCSBase::SubmitFileModel *model)
72 {
73     typedef QList<CommitData::StateFilePair>::const_iterator ConstIterator;
74     if (!l.empty()) {
75         const ConstIterator cend = l.constEnd();
76         const QVariant fileTypeData(ft);
77         for (ConstIterator it = l.constBegin(); it != cend; ++it)
78             model->addFile(it->second, it->first, checked).front()->setData(fileTypeData, FileTypeRole);
79     }
80 }
81
82 void GitSubmitEditor::setCommitData(const CommitData &d)
83 {
84     submitEditorWidget()->setPanelData(d.panelData);
85     submitEditorWidget()->setPanelInfo(d.panelInfo);
86
87     m_commitEncoding = d.commitEncoding;
88
89     m_model = new VCSBase::SubmitFileModel(this);
90     addStateFileListToModel(d.stagedFiles,   true,  StagedFile,   m_model);
91     addStateFileListToModel(d.unstagedFiles, false, UnstagedFile, m_model);
92     if (!d.untrackedFiles.empty()) {
93         const QString untrackedSpec = QLatin1String("untracked");
94         const QVariant fileTypeData(UntrackedFile);
95         const QStringList::const_iterator cend = d.untrackedFiles.constEnd();
96         for (QStringList::const_iterator it = d.untrackedFiles.constBegin(); it != cend; ++it)
97             m_model->addFile(*it, untrackedSpec, false).front()->setData(fileTypeData, FileTypeRole);
98     }
99     setFileModel(m_model);
100 }
101
102 void GitSubmitEditor::slotDiffSelected(const QStringList &files)
103 {
104     // Sort it apart into staged/unstaged files
105     QStringList unstagedFiles;
106     QStringList stagedFiles;
107     const int fileColumn = fileNameColumn();
108     const int rowCount = m_model->rowCount();
109     for (int r = 0; r < rowCount; r++) {
110         const QString fileName = m_model->item(r, fileColumn)->text();
111         if (files.contains(fileName)) {
112             const FileType ft = static_cast<FileType>(m_model->item(r, 0)->data(FileTypeRole).toInt());
113             switch (ft) {
114             case StagedFile:
115                 stagedFiles.push_back(fileName);
116                 break;
117             case UnstagedFile:
118                 unstagedFiles.push_back(fileName);
119                 break;
120             case UntrackedFile:
121                 break;
122             }
123         }
124     }
125     if (!unstagedFiles.empty() || !stagedFiles.empty())
126         emit diff(unstagedFiles, stagedFiles);
127 }
128
129 GitSubmitEditorPanelData GitSubmitEditor::panelData() const
130 {
131     return const_cast<GitSubmitEditor*>(this)->submitEditorWidget()->panelData();
132 }
133
134 QByteArray GitSubmitEditor::fileContents() const
135 {
136     const QString& text = const_cast<GitSubmitEditor*>(this)->submitEditorWidget()->descriptionText();
137
138     if (!m_commitEncoding.isEmpty()) {
139         // Do the encoding convert, When use user-defined encoding
140         // e.g. git config --global i18n.commitencoding utf-8
141         QTextCodec *codec = QTextCodec::codecForName(m_commitEncoding.toLocal8Bit());
142         if (codec)
143             return codec->fromUnicode(text);
144     }
145
146     // Using utf-8 as the default encoding
147     return text.toUtf8();
148 }
149
150 } // namespace Internal
151 } // namespace Git