Don't allow to unrecord past the branch point
[vng.git] / hunks / File.cpp
blob07d04ff0537405203d64dbaaf5e49aa6948384e7
1 /*
2 * This file is part of the vng project
3 * Copyright (C) 2008 Thomas Zander <tzander@trolltech.com>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "File.h"
21 class File::Private {
22 public:
23 Private() : ref(1), renameAcceptance(Vng::Undecided), protectionAcceptance(Vng::Undecided), isBinaryFile(false)
27 QString protection, oldProtection;
28 QString sha1, oldSha1;
29 QByteArray filename;
30 QByteArray oldFilename;
31 QList<Hunk> hunks;
32 int ref;
33 Vng::Acceptance renameAcceptance;
34 Vng::Acceptance protectionAcceptance;
35 bool isBinaryFile;
38 File::File()
39 : d(new Private())
43 File::File(const File &other)
44 : d(other.d)
46 d->ref++;
49 File::~File()
51 if (--d->ref == 0)
52 delete d;
55 void File::addHunk(const Hunk &hunk)
57 if (! hunk.isEmpty())
58 d->hunks << hunk;
61 QList<Hunk> File::hunks() const
63 return d->hunks;
66 bool File::isValid() const
68 return !d->filename.isEmpty();
71 void File::setFileName(const QByteArray &filename)
73 d->filename = filename;
74 d->renameAcceptance = d->oldFilename == d->filename ? Vng::Accepted : Vng::Undecided;
77 QByteArray File::fileName() const
79 return d->filename;
82 File & File::operator=(const File &other)
84 other.d->ref++;
85 if (--d->ref == 0)
86 delete d;
87 d = other.d;
88 return *this;
91 bool File::operator==(const File &other)
93 return other.d == d;
96 int File::count() const
98 return d->hunks.count();
101 void File::setOldFileName(const QByteArray &filename)
103 d->oldFilename = filename;
104 d->renameAcceptance = d->oldFilename == d->filename ? Vng::Accepted : Vng::Undecided;
107 QByteArray File::oldFileName() const
109 return d->oldFilename;
112 void File::setProtection(const QString &string)
114 d->protection = string;
115 d->protectionAcceptance = string == d->oldProtection ? Vng::Accepted : Vng::Undecided;
118 void File::setOldProtection(const QString &string)
120 d->oldProtection = string;
121 d->protectionAcceptance = string == d->protection ? Vng::Accepted : Vng::Undecided;
124 QString File::protection() const
126 return d->protection;
129 QString File::oldProtection() const
131 return d->oldProtection;
135 void File::setOldSha1(const QString &string)
137 d->oldSha1 = string;
140 void File::setSha1(const QString &string)
142 d->sha1 = string;
145 QString File::oldSha1() const
147 return d->oldSha1;
150 QString File::sha1() const
152 return d->sha1;
155 bool File::hasChanged() const
157 return count() || d->protection != d->oldProtection ||
158 d->filename != d->oldFilename;
161 void File::setRenameAcceptance(Vng::Acceptance accepted)
163 if (d->filename != d->oldFilename)
164 d->renameAcceptance = accepted;
167 void File::setProtectionAcceptance(Vng::Acceptance accepted)
169 if (d->protection != d->oldProtection)
170 d->protectionAcceptance = accepted;
173 Vng::Acceptance File::renameAcceptance() const
175 return d->renameAcceptance;
178 Vng::Acceptance File::protectionAcceptance() const
180 return d->protectionAcceptance;
183 QFile::Permissions File::permissions() const
185 QFile::Permissions perms;
186 if (d->protection.length() != 6)
187 return perms;
188 Q_ASSERT(d->protection.length() == 6);
189 const int user = d->protection.mid(3,1).toInt();
190 if (user & 1) perms |= QFile::ExeOwner;
191 if (user & 2) perms |= QFile::WriteOwner;
192 if (user & 4) perms |= QFile::ReadOwner;
193 const int group = d->protection.mid(4,1).toInt();
194 if (group & 1) perms |= QFile::ExeGroup;
195 if (group & 2) perms |= QFile::WriteGroup;
196 if (group & 4) perms |= QFile::ReadGroup;
197 const int other = d->protection.mid(5,1).toInt();
198 if (other & 1) perms |= QFile::ExeOther;
199 if (other & 2) perms |= QFile::WriteOther;
200 if (other & 4) perms |= QFile::ReadOther;
201 return perms;
204 int File::linesAdded() const
206 int total = 0;
207 foreach(Hunk hunk, d->hunks)
208 total += hunk.linesAdded();
209 return total;
212 int File::linesRemoved() const
214 int total = 0;
215 foreach(Hunk hunk, d->hunks)
216 total += hunk.linesRemoved();
217 return total;
220 void File::setBinary(bool binary)
222 d->isBinaryFile = binary;
225 bool File::isBinary() const
227 return d->isBinaryFile;