[go: up one dir, main page]

File: domain.cpp

package info (click to toggle)
qgit 2.3-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,152 kB
  • ctags: 1,477
  • sloc: cpp: 11,857; makefile: 51; sh: 39
file content (385 lines) | stat: -rw-r--r-- 8,715 bytes parent folder | download | duplicates (2)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
	Author: Marco Costalba (C) 2005-2007

	Copyright: See COPYING file that comes with this distribution

*/
#include <QApplication>
#include <QStatusBar>
#include <QTimer>
#include "exceptionmanager.h"
#include "mainimpl.h"
#include "git.h"
#include "domain.h"

using namespace QGit;

void StateInfo::S::clear() {

	sha = fn = dtSha = "";
	isM = allM = false;
	sel = true;
}

bool StateInfo::S::operator==(const StateInfo::S& st) const {

	if (&st == this)
		return true;

	return (   sha   == st.sha
	        && fn    == st.fn
	        && dtSha == st.dtSha
	        && sel   == st.sel
	        && isM   == st.isM
	        && allM  == st.allM);
}

bool StateInfo::S::operator!=(const StateInfo::S& st) const {

	return !(StateInfo::S::operator==(st));
}

void StateInfo::clear() {

	nextS.clear();
	curS.clear();
	prevS.clear();
	isLocked = false;
}

StateInfo& StateInfo::operator=(const StateInfo& newState) {

	if (&newState != this) {
		if (isLocked)
			nextS = newState.curS;
		else
			curS = newState.curS; // prevS is mot modified to allow a rollback
	}
	return *this;
}

bool StateInfo::operator==(const StateInfo& newState) const {

	if (&newState == this)
		return true;

	return (curS == newState.curS); // compare is made on curS only
}

bool StateInfo::operator!=(const StateInfo& newState) const {

	return !(StateInfo::operator==(newState));
}

bool StateInfo::isChanged(uint what) const {

	bool ret = false;
	if (what & SHA)
		ret = (sha(true) != sha(false));

	if (!ret && (what & FILE_NAME))
		ret = (fileName(true) != fileName(false));

	if (!ret && (what & DIFF_TO_SHA))
		ret = (diffToSha(true) != diffToSha(false));

	if (!ret && (what & ALL_MERGE_FILES))
		ret = (allMergeFiles(true) != allMergeFiles(false));

	return ret;
}

// ************************* Domain ****************************

Domain::Domain(MainImpl* m, Git* g, bool isMain) : QObject(m), git(g) {

	EM_INIT(exDeleteRequest, "Deleting domain");
	EM_INIT(exCancelRequest, "Canceling update");

	fileHistory = new FileHistory(this, git);
	if (isMain)
		git->setDefaultModel(fileHistory);

	st.clear();
	busy = readyToDrag = dragging = dropping = linked = false;
	popupType = 0;
	container = new QWidget(NULL); // will be reparented to m()->tabWdg
}

Domain::~Domain() {

	if (!parent())
		return;

	// remove before to delete, avoids a Qt warning in QInputContext()
	m()->tabWdg->removeTab(m()->tabWdg->indexOf(container));
	container->deleteLater();
}

void Domain::clear(bool complete) {

	if (complete)
		st.clear();

	fileHistory->clear();
}

void Domain::on_closeAllTabs() {

	delete this; // must be sync, deleteLater() does not work
}

void Domain::deleteWhenDone() {

	if (!EM_IS_PENDING(exDeleteRequest))
		EM_RAISE(exDeleteRequest);

	emit cancelDomainProcesses();

	on_deleteWhenDone();
}

void Domain::on_deleteWhenDone() {

	if (!EM_IS_PENDING(exDeleteRequest))
		deleteLater();
	else
		QTimer::singleShot(20, this, SLOT(on_deleteWhenDone()));
}

void Domain::setThrowOnDelete(bool b) {

	if (b)
		EM_REGISTER(exDeleteRequest);
	else
		EM_REMOVE(exDeleteRequest);
}

bool Domain::isThrowOnDeleteRaised(int excpId, SCRef curContext) {

	return EM_MATCH(excpId, exDeleteRequest, curContext);
}

MainImpl* Domain::m() const {

	return static_cast<MainImpl*>(parent());
}

void Domain::showStatusBarMessage(const QString& msg, int timeout) {

	m()->statusBar()->showMessage(msg, timeout);
}

void Domain::setTabCaption(const QString& caption) {

	int idx = m()->tabWdg->indexOf(container);
	m()->tabWdg->setTabText(idx, caption);
}

bool Domain::setReadyToDrag(bool b) {

	readyToDrag = (b && !busy && !dragging && !dropping);
	return readyToDrag;
}

bool Domain::setDragging(bool b) {

	bool dragFinished = (!b && dragging);

	dragging = (b && readyToDrag && !dropping);

	if (dragging)
		readyToDrag = false;

	if (dragFinished)
		flushQueue();

	return dragging;
}

void Domain::unlinkDomain(Domain* d) {

	d->linked = false;
	while (d->disconnect(SIGNAL(updateRequested(StateInfo)), this))
		;// a signal is emitted for every connection you make,
		 // so if you duplicate a connection, two signals will be emitted.
}

void Domain::linkDomain(Domain* d) {

	unlinkDomain(d); // be sure only one connection is active
	connect(d, SIGNAL(updateRequested(StateInfo)), this, SLOT(on_updateRequested(StateInfo)));
	d->linked = true;
}

void Domain::on_updateRequested(StateInfo newSt) {

	st = newSt;
	UPDATE();
}

bool Domain::flushQueue() {
// during dragging any state update is queued, so try to flush pending now

	if (!busy && st.flushQueue()) {
		UPDATE();
		return true;
	}
	return false;
}

bool Domain::event(QEvent* e) {

	bool fromMaster = false;

	switch (e->type()) {
	case UPD_DM_MST_EV:
		fromMaster = true;
		// fall through
	case UPD_DM_EV:
		update(fromMaster, ((UpdateDomainEvent*)e)->isForced());
		break;
	case MSG_EV:
		if (!busy && !st.requestPending())
			QApplication::postEvent(m(), new MessageEvent(((MessageEvent*)e)->myData()));
		else // waiting for the end of updating
			statusBarRequest = ((MessageEvent*)e)->myData();
		break;
	default:
		break;
	}
	return QObject::event(e);
}

void Domain::populateState() {

	const Rev* r = git->revLookup(st.sha());
	if (r)
		st.setIsMerge(r->parentsCount() > 1);
}

void Domain::update(bool fromMaster, bool force) {

	if (busy && st.requestPending()) {
		// quick exit current (obsoleted) update but only if state
		// is going to change. Without this check calling update()
		// many times with the same data nullify the update
		EM_RAISE(exCancelRequest);
		emit cancelDomainProcesses();
	}
	if (busy || dragging)
		return;

	if (linked && !fromMaster) {
		// in this case let the update to fall down from master domain
		StateInfo tmp(st);
		st.rollBack(); // we don't want to filter out next update sent from master
		emit updateRequested(tmp);
		return;
	}
	try {
		EM_REGISTER_Q(exCancelRequest); // quiet, no messages when thrown
		setThrowOnDelete(true);
		git->setThrowOnStop(true);
		git->setCurContext(this);
		busy = true;
		setReadyToDrag(false); // do not start any drag while updating
		populateState(); // complete any missing state information
		st.setLock(true); // any state change will be queued now

		if (doUpdate(force))
			st.commit();
		else
			st.rollBack();

		st.setLock(false);
		busy = false;
		if (git->curContext() != this)
			qDebug("ASSERT in Domain::update, context is %p "
			       "instead of %p", (void*)git->curContext(), (void*)this);

		git->setCurContext(NULL);
		git->setThrowOnStop(false);
		setThrowOnDelete(false);
		EM_REMOVE(exCancelRequest);

	} catch (int i) {

		/*
		   If we have a cancel request because of a new update is in queue we
		   cannot roolback current state to avoid new update is filtered out
		   in case rolled back sha and new sha are the same.
		   This could happen with arrow navigation:

		   sha -> go UP (new sha) -> go DOWN (cancel) -> rollback to sha

		   And pending request 'sha' is filtered out leaving us in an
		   inconsistent state.
		*/
		st.commit();
		st.setLock(false);
		busy = false;
		git->setCurContext(NULL);
		git->setThrowOnStop(false);
		setThrowOnDelete(false);
		EM_REMOVE(exCancelRequest);

		if (QApplication::overrideCursor())
			QApplication::restoreOverrideCursor();

		QString context("updating ");
		if (git->isThrowOnStopRaised(i,  context + metaObject()->className())) {
			EM_THROW_PENDING;
			return;
		}
		if (isThrowOnDeleteRaised(i,  context + metaObject()->className())) {
			EM_THROW_PENDING;
			return;
		}
		if (i == exCancelRequest)
			EM_THROW_PENDING;
			// do not return
		else {
			const QString info("Exception \'" + EM_DESC(i) + "\' "
			                   "not handled in init...re-throw");
			dbs(info);
			throw;
		}
	}
	bool nextRequestPending = flushQueue();

	if (!nextRequestPending && !statusBarRequest.isEmpty()) {
		// update status bar when we are sure no more work is pending
		QApplication::postEvent(m(), new MessageEvent(statusBarRequest));
		statusBarRequest = "";
	}
	if (!nextRequestPending && popupType)
		sendPopupEvent();
}

void Domain::sendPopupEvent() {

	// call an async context popup, must be executed
	// after returning to event loop
	DeferredPopupEvent* e = new DeferredPopupEvent(popupData, popupType);
	QApplication::postEvent(m(), e);
	popupType = 0;
}

void Domain::on_contextMenu(const QString& data, int type) {

	popupType = type;
	popupData = data;

	if (busy)
		return; // we are in the middle of an update

	// if list view is already updated pop-up
	// context menu, otherwise it means update()
	// has still not been called, a pop-up request,
	// will be fired up at the end of next update()
	if ((type == POPUP_LIST_EV) && (data != st.sha()))
		return;

	sendPopupEvent();
}