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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
|
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms and
** conditions see http://www.qt.io/terms-conditions. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "session.h"
#include "project.h"
#include "target.h"
#include "kit.h"
#include "buildconfiguration.h"
#include "deployconfiguration.h"
#include "projectexplorer.h"
#include "nodesvisitor.h"
#include "editorconfiguration.h"
#include "projectnodes.h"
#include <coreplugin/icore.h>
#include <coreplugin/idocument.h>
#include <coreplugin/imode.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/progressmanager/progressmanager.h>
#include <coreplugin/modemanager.h>
#include <texteditor/texteditor.h>
#include <utils/algorithm.h>
#include <utils/stylehelper.h>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QMessageBox>
#include <QPushButton>
namespace { bool debug = false; }
using namespace Core;
using namespace Utils;
using namespace ProjectExplorer::Internal;
namespace ProjectExplorer {
/*!
\class ProjectExplorer::SessionManager
\brief The SessionManager class manages sessions.
TODO the interface of this class is not really great.
The implementation suffers from that all the functions from the
public interface just wrap around functions which do the actual work.
This could be improved.
*/
class SessionManagerPrivate
{
public:
SessionManagerPrivate() :
m_sessionName(QLatin1String("default")),
m_virginSession(true),
m_loadingSession(false),
m_startupProject(0),
m_writer(0)
{}
bool projectContainsFile(Project *p, const FileName &fileName) const;
void restoreValues(const PersistentSettingsReader &reader);
void restoreDependencies(const PersistentSettingsReader &reader);
void restoreStartupProject(const PersistentSettingsReader &reader);
void restoreEditors(const PersistentSettingsReader &reader);
void restoreProjects(const QStringList &fileList);
void askUserAboutFailedProjects();
void sessionLoadingProgress();
bool recursiveDependencyCheck(const QString &newDep, const QString &checkDep) const;
QStringList dependencies(const QString &proName) const;
QStringList dependenciesOrder() const;
void dependencies(const QString &proName, QStringList &result) const;
public:
static QString windowTitleAddition(const QString &filePath);
SessionNode *m_sessionNode;
QString m_sessionName;
bool m_virginSession;
mutable QStringList m_sessions;
mutable QHash<Project *, QStringList> m_projectFileCache;
bool m_loadingSession;
bool m_casadeSetActive;
Project *m_startupProject;
QList<Project *> m_projects;
QStringList m_failedProjects;
QMap<QString, QStringList> m_depMap;
QMap<QString, QVariant> m_values;
QFutureInterface<void> m_future;
PersistentSettingsWriter *m_writer;
};
static SessionManager *m_instance = 0;
static SessionManagerPrivate *d = 0;
SessionManager::SessionManager(QObject *parent)
: QObject(parent)
{
m_instance = this;
d = new SessionManagerPrivate;
d->m_sessionNode = new SessionNode;
connect(ModeManager::instance(), &ModeManager::currentModeChanged,
this, &SessionManager::saveActiveMode);
connect(EditorManager::instance(), &EditorManager::editorCreated,
this, &SessionManager::configureEditor);
connect(this, &SessionManager::projectAdded,
EditorManager::instance(), &EditorManager::updateWindowTitles);
connect(this, &SessionManager::projectRemoved,
EditorManager::instance(), &EditorManager::updateWindowTitles);
connect(this, &SessionManager::projectDisplayNameChanged,
EditorManager::instance(), &EditorManager::updateWindowTitles);
connect(EditorManager::instance(), &EditorManager::editorOpened,
[this] { markSessionFileDirty(); });
connect(EditorManager::instance(), &EditorManager::editorsClosed,
[this] { markSessionFileDirty(); });
EditorManager::setWindowTitleAdditionHandler(&SessionManagerPrivate::windowTitleAddition);
}
SessionManager::~SessionManager()
{
emit m_instance->aboutToUnloadSession(d->m_sessionName);
delete d->m_writer;
delete d->m_sessionNode;
delete d;
}
SessionManager *SessionManager::instance()
{
return m_instance;
}
bool SessionManager::isDefaultVirgin()
{
return isDefaultSession(d->m_sessionName) && d->m_virginSession;
}
bool SessionManager::isDefaultSession(const QString &session)
{
return session == QLatin1String("default");
}
void SessionManager::saveActiveMode(IMode *mode)
{
if (mode->id() != Id(Core::Constants::MODE_WELCOME))
setValue(QLatin1String("ActiveMode"), mode->id().toString());
}
void SessionManager::clearProjectFileCache()
{
// If triggered by the fileListChanged signal of one project
// only invalidate cache for this project
Project *pro = qobject_cast<Project*>(m_instance->sender());
if (pro)
d->m_projectFileCache.remove(pro);
else
d->m_projectFileCache.clear();
}
bool SessionManagerPrivate::recursiveDependencyCheck(const QString &newDep, const QString &checkDep) const
{
if (newDep == checkDep)
return false;
foreach (const QString &dependency, m_depMap.value(checkDep))
if (!recursiveDependencyCheck(newDep, dependency))
return false;
return true;
}
/*
* The dependency management exposes an interface based on projects, but
* is internally purely string based. This is suboptimal. Probably it would be
* nicer to map the filenames to projects on load and only map it back to
* filenames when saving.
*/
QList<Project *> SessionManager::dependencies(const Project *project)
{
const QString proName = project->projectFilePath().toString();
const QStringList proDeps = d->m_depMap.value(proName);
QList<Project *> projects;
foreach (const QString &dep, proDeps) {
if (Project *pro = projectForFile(Utils::FileName::fromString(dep)))
projects += pro;
}
return projects;
}
bool SessionManager::hasDependency(const Project *project, const Project *depProject)
{
const QString proName = project->projectFilePath().toString();
const QString depName = depProject->projectFilePath().toString();
const QStringList proDeps = d->m_depMap.value(proName);
return proDeps.contains(depName);
}
bool SessionManager::canAddDependency(const Project *project, const Project *depProject)
{
const QString newDep = project->projectFilePath().toString();
const QString checkDep = depProject->projectFilePath().toString();
return d->recursiveDependencyCheck(newDep, checkDep);
}
bool SessionManager::addDependency(Project *project, Project *depProject)
{
const QString proName = project->projectFilePath().toString();
const QString depName = depProject->projectFilePath().toString();
// check if this dependency is valid
if (!d->recursiveDependencyCheck(proName, depName))
return false;
QStringList proDeps = d->m_depMap.value(proName);
if (!proDeps.contains(depName)) {
proDeps.append(depName);
d->m_depMap[proName] = proDeps;
}
emit m_instance->dependencyChanged(project, depProject);
return true;
}
void SessionManager::removeDependency(Project *project, Project *depProject)
{
const QString proName = project->projectFilePath().toString();
const QString depName = depProject->projectFilePath().toString();
QStringList proDeps = d->m_depMap.value(proName);
proDeps.removeAll(depName);
if (proDeps.isEmpty())
d->m_depMap.remove(proName);
else
d->m_depMap[proName] = proDeps;
emit m_instance->dependencyChanged(project, depProject);
}
bool SessionManager::isProjectConfigurationCascading()
{
return d->m_casadeSetActive;
}
void SessionManager::setProjectConfigurationCascading(bool b)
{
d->m_casadeSetActive = b;
markSessionFileDirty();
}
void SessionManager::setActiveTarget(Project *project, Target *target, SetActive cascade)
{
QTC_ASSERT(project, return);
project->setActiveTarget(target);
if (!target) // never cascade setting no target
return;
if (cascade != SetActive::Cascade || !d->m_casadeSetActive)
return;
Core::Id kitId = target->kit()->id();
foreach (Project *otherProject, SessionManager::projects()) {
if (otherProject == project)
continue;
foreach (Target *otherTarget, otherProject->targets()) {
if (otherTarget->kit()->id() == kitId) {
otherProject->setActiveTarget(otherTarget);
break;
}
}
}
}
void SessionManager::setActiveBuildConfiguration(Target *target, BuildConfiguration *bc, SetActive cascade)
{
QTC_ASSERT(target, return);
target->setActiveBuildConfiguration(bc);
if (!bc)
return;
if (cascade != SetActive::Cascade || !d->m_casadeSetActive)
return;
Core::Id kitId = target->kit()->id();
QString name = bc->displayName(); // We match on displayname
foreach (Project *otherProject, SessionManager::projects()) {
if (otherProject == target->project())
continue;
Target *otherTarget = otherProject->activeTarget();
if (otherTarget->kit()->id() != kitId)
continue;
foreach (BuildConfiguration *otherBc, otherTarget->buildConfigurations()) {
if (otherBc->displayName() == name) {
otherTarget->setActiveBuildConfiguration(otherBc);
break;
}
}
}
}
void SessionManager::setActiveDeployConfiguration(Target *target, DeployConfiguration *dc, SetActive cascade)
{
QTC_ASSERT(target, return);
target->setActiveDeployConfiguration(dc);
if (!dc)
return;
if (cascade != SetActive::Cascade || !d->m_casadeSetActive)
return;
Core::Id kitId = target->kit()->id();
QString name = dc->displayName(); // We match on displayname
foreach (Project *otherProject, SessionManager::projects()) {
if (otherProject == target->project())
continue;
Target *otherTarget = otherProject->activeTarget();
if (otherTarget->kit()->id() != kitId)
continue;
foreach (DeployConfiguration *otherDc, otherTarget->deployConfigurations()) {
if (otherDc->displayName() == name) {
otherTarget->setActiveDeployConfiguration(otherDc);
break;
}
}
}
}
void SessionManager::setStartupProject(Project *startupProject)
{
if (debug)
qDebug() << Q_FUNC_INFO << (startupProject ? startupProject->displayName() : QLatin1String("0"));
if (startupProject) {
Q_ASSERT(d->m_projects.contains(startupProject));
}
if (d->m_startupProject == startupProject)
return;
d->m_startupProject = startupProject;
emit m_instance->startupProjectChanged(startupProject);
}
Project *SessionManager::startupProject()
{
return d->m_startupProject;
}
void SessionManager::addProject(Project *project)
{
addProjects(QList<Project*>() << project);
}
void SessionManager::addProjects(const QList<Project*> &projects)
{
d->m_virginSession = false;
QList<Project*> clearedList;
foreach (Project *pro, projects) {
if (!d->m_projects.contains(pro)) {
clearedList.append(pro);
d->m_projects.append(pro);
d->m_sessionNode->addProjectNodes(QList<ProjectNode *>() << pro->rootProjectNode());
connect(pro, &Project::fileListChanged,
m_instance, &SessionManager::clearProjectFileCache);
connect(pro, &Project::displayNameChanged,
m_instance, &SessionManager::handleProjectDisplayNameChanged);
if (debug)
qDebug() << "SessionManager - adding project " << pro->displayName();
}
}
foreach (Project *pro, clearedList) {
emit m_instance->projectAdded(pro);
configureEditors(pro);
connect(pro, &Project::fileListChanged,
[pro](){
configureEditors(pro);
});
}
if (clearedList.count() == 1)
emit m_instance->singleProjectAdded(clearedList.first());
}
void SessionManager::removeProject(Project *project)
{
d->m_virginSession = false;
if (project == 0) {
qDebug() << "SessionManager::removeProject(0) ... THIS SHOULD NOT HAPPEN";
return;
}
removeProjects(QList<Project*>() << project);
}
bool SessionManager::loadingSession()
{
return d->m_loadingSession;
}
bool SessionManager::save()
{
if (debug)
qDebug() << "SessionManager - saving session" << d->m_sessionName;
emit m_instance->aboutToSaveSession();
if (!d->m_writer || d->m_writer->fileName() != sessionNameToFileName(d->m_sessionName)) {
delete d->m_writer;
d->m_writer = new PersistentSettingsWriter(sessionNameToFileName(d->m_sessionName),
QLatin1String("QtCreatorSession"));
}
QVariantMap data;
// save the startup project
if (d->m_startupProject)
data.insert(QLatin1String("StartupProject"), d->m_startupProject->projectFilePath().toString());
QColor c = StyleHelper::requestedBaseColor();
if (c.isValid()) {
QString tmp = QString::fromLatin1("#%1%2%3")
.arg(c.red(), 2, 16, QLatin1Char('0'))
.arg(c.green(), 2, 16, QLatin1Char('0'))
.arg(c.blue(), 2, 16, QLatin1Char('0'));
data.insert(QLatin1String("Color"), tmp);
}
QStringList projectFiles;
foreach (Project *pro, d->m_projects)
projectFiles << pro->projectFilePath().toString();
// Restore infromation on projects that failed to load:
// don't readd projects to the list, which the user loaded
foreach (const QString &failed, d->m_failedProjects)
if (!projectFiles.contains(failed))
projectFiles << failed;
data.insert(QLatin1String("ProjectList"), projectFiles);
data.insert(QLatin1String("CascadeSetActive"), d->m_casadeSetActive);
QMap<QString, QVariant> depMap;
QMap<QString, QStringList>::const_iterator i = d->m_depMap.constBegin();
while (i != d->m_depMap.constEnd()) {
QString key = i.key();
QStringList values;
foreach (const QString &value, i.value()) {
values << value;
}
depMap.insert(key, values);
++i;
}
data.insert(QLatin1String("ProjectDependencies"), QVariant(depMap));
data.insert(QLatin1String("EditorSettings"), EditorManager::saveState().toBase64());
QMap<QString, QVariant>::const_iterator it, end = d->m_values.constEnd();
QStringList keys;
for (it = d->m_values.constBegin(); it != end; ++it) {
data.insert(QLatin1String("value-") + it.key(), it.value());
keys << it.key();
}
data.insert(QLatin1String("valueKeys"), keys);
bool result = d->m_writer->save(data, ICore::mainWindow());
if (!result) {
QMessageBox::warning(ICore::dialogParent(), tr("Error while saving session"),
tr("Could not save session to file %1").arg(d->m_writer->fileName().toUserOutput()));
}
if (debug)
qDebug() << "SessionManager - saving session returned " << result;
return result;
}
/*!
Closes all projects
*/
void SessionManager::closeAllProjects()
{
setStartupProject(0);
removeProjects(projects());
}
QList<Project *> SessionManager::projects()
{
return d->m_projects;
}
bool SessionManager::hasProjects()
{
return !d->m_projects.isEmpty();
}
QStringList SessionManagerPrivate::dependencies(const QString &proName) const
{
QStringList result;
dependencies(proName, result);
return result;
}
void SessionManagerPrivate::dependencies(const QString &proName, QStringList &result) const
{
QStringList depends = m_depMap.value(proName);
foreach (const QString &dep, depends)
dependencies(dep, result);
if (!result.contains(proName))
result.append(proName);
}
QString SessionManagerPrivate::windowTitleAddition(const QString &filePath)
{
if (SessionManager::isDefaultSession(d->m_sessionName)) {
if (filePath.isEmpty()) {
// use single project's name if there is only one loaded.
const QList<Project *> projects = SessionManager::projects();
if (projects.size() == 1)
return projects.first()->displayName();
return QString();
} else if (Project *project = SessionManager::projectForFile(
Utils::FileName::fromString(filePath))) {
return project->displayName();
} else {
return QString();
}
} else {
QString sessionName = d->m_sessionName;
if (sessionName.isEmpty())
sessionName = SessionManager::tr("Untitled");
return sessionName;
}
}
QStringList SessionManagerPrivate::dependenciesOrder() const
{
QList<QPair<QString, QStringList> > unordered;
QStringList ordered;
// copy the map to a temporary list
foreach (Project *pro, m_projects) {
const QString proName = pro->projectFilePath().toString();
unordered << QPair<QString, QStringList>(proName, m_depMap.value(proName));
}
while (!unordered.isEmpty()) {
for (int i=(unordered.count()-1); i>=0; --i) {
if (unordered.at(i).second.isEmpty()) {
ordered << unordered.at(i).first;
unordered.removeAt(i);
}
}
// remove the handled projects from the dependency lists
// of the remaining unordered projects
for (int i = 0; i < unordered.count(); ++i) {
foreach (const QString &pro, ordered) {
QStringList depList = unordered.at(i).second;
depList.removeAll(pro);
unordered[i].second = depList;
}
}
}
return ordered;
}
QList<Project *> SessionManager::projectOrder(Project *project)
{
QList<Project *> result;
QStringList pros;
if (project)
pros = d->dependencies(project->projectFilePath().toString());
else
pros = d->dependenciesOrder();
foreach (const QString &proFile, pros) {
foreach (Project *pro, projects()) {
if (pro->projectFilePath().toString() == proFile) {
result << pro;
break;
}
}
}
return result;
}
QList<Node *> SessionManager::nodesForFile(const Utils::FileName &fileName)
{
FindNodesForFileVisitor findNodes(fileName);
sessionNode()->accept(&findNodes);
return findNodes.nodes();
}
// node for file returns a randomly selected node if there are multiple
// prefer to use nodesForFile and figure out which node you want
Node *SessionManager::nodeForFile(const Utils::FileName &fileName)
{
Node *node = 0;
foreach (Node *n, nodesForFile(fileName)) {
// prefer file nodes
if (!node || (node->nodeType() != FileNodeType && n->nodeType() == FileNodeType))
node = n;
}
return node;
}
Project *SessionManager::projectForNode(Node *node)
{
if (!node)
return 0;
FolderNode *rootProjectNode = node->asFolderNode();
if (!rootProjectNode)
rootProjectNode = node->parentFolderNode();
while (rootProjectNode && rootProjectNode->parentFolderNode() != d->m_sessionNode)
rootProjectNode = rootProjectNode->parentFolderNode();
Q_ASSERT(rootProjectNode);
return Utils::findOrDefault(d->m_projects, Utils::equal(&Project::rootProjectNode, rootProjectNode));
}
Project *SessionManager::projectForFile(const Utils::FileName &fileName)
{
if (debug)
qDebug() << "SessionManager::projectForFile(" << fileName << ")";
const QList<Project *> &projectList = projects();
foreach (Project *p, projectList)
if (d->projectContainsFile(p, fileName))
return p;
return 0;
}
bool SessionManagerPrivate::projectContainsFile(Project *p, const Utils::FileName &fileName) const
{
if (!m_projectFileCache.contains(p))
m_projectFileCache.insert(p, p->files(Project::AllFiles));
return m_projectFileCache.value(p).contains(fileName.toString());
}
void SessionManager::configureEditor(IEditor *editor, const QString &fileName)
{
if (TextEditor::BaseTextEditor *textEditor = qobject_cast<TextEditor::BaseTextEditor*>(editor)) {
Project *project = projectForFile(Utils::FileName::fromString(fileName));
// Global settings are the default.
if (project)
project->editorConfiguration()->configureEditor(textEditor);
}
}
void SessionManager::configureEditors(Project *project)
{
foreach (IDocument *document, DocumentModel::openedDocuments()) {
if (d->projectContainsFile(project, document->filePath())) {
foreach (IEditor *editor, DocumentModel::editorsForDocument(document)) {
if (TextEditor::BaseTextEditor *textEditor = qobject_cast<TextEditor::BaseTextEditor*>(editor)) {
project->editorConfiguration()->configureEditor(textEditor);
}
}
}
}
}
void SessionManager::removeProjects(QList<Project *> remove)
{
QMap<QString, QStringList> resMap;
foreach (Project *pro, remove) {
if (debug)
qDebug() << "SessionManager - emitting aboutToRemoveProject(" << pro->displayName() << ")";
emit m_instance->aboutToRemoveProject(pro);
}
// Refresh dependencies
QSet<QString> projectFiles;
foreach (Project *pro, projects()) {
if (!remove.contains(pro))
projectFiles.insert(pro->projectFilePath().toString());
}
QSet<QString>::const_iterator i = projectFiles.begin();
while (i != projectFiles.end()) {
QStringList dependencies;
foreach (const QString &dependency, d->m_depMap.value(*i)) {
if (projectFiles.contains(dependency))
dependencies << dependency;
}
if (!dependencies.isEmpty())
resMap.insert(*i, dependencies);
++i;
}
d->m_depMap = resMap;
// TODO: Clear m_modelProjectHash
// Delete projects
foreach (Project *pro, remove) {
pro->saveSettings();
d->m_projects.removeOne(pro);
if (pro == d->m_startupProject)
setStartupProject(0);
disconnect(pro, &Project::fileListChanged,
m_instance, &SessionManager::clearProjectFileCache);
d->m_projectFileCache.remove(pro);
if (debug)
qDebug() << "SessionManager - emitting projectRemoved(" << pro->displayName() << ")";
d->m_sessionNode->removeProjectNodes(QList<ProjectNode *>() << pro->rootProjectNode());
emit m_instance->projectRemoved(pro);
delete pro;
}
if (startupProject() == 0)
if (!d->m_projects.isEmpty())
setStartupProject(d->m_projects.first());
}
/*!
Lets other plugins store persistent values within the session file.
*/
void SessionManager::setValue(const QString &name, const QVariant &value)
{
if (d->m_values.value(name) == value)
return;
d->m_values.insert(name, value);
markSessionFileDirty(false);
}
QVariant SessionManager::value(const QString &name)
{
QMap<QString, QVariant>::const_iterator it = d->m_values.constFind(name);
return (it == d->m_values.constEnd()) ? QVariant() : *it;
}
QString SessionManager::activeSession()
{
return d->m_sessionName;
}
QStringList SessionManager::sessions()
{
if (d->m_sessions.isEmpty()) {
// We are not initialized yet, so do that now
QDir sessionDir(ICore::userResourcePath());
QList<QFileInfo> sessionFiles = sessionDir.entryInfoList(QStringList() << QLatin1String("*.qws"), QDir::NoFilter, QDir::Time);
foreach (const QFileInfo &fileInfo, sessionFiles) {
if (fileInfo.completeBaseName() != QLatin1String("default"))
d->m_sessions << fileInfo.completeBaseName();
}
d->m_sessions.prepend(QLatin1String("default"));
}
return d->m_sessions;
}
FileName SessionManager::sessionNameToFileName(const QString &session)
{
return FileName::fromString(ICore::userResourcePath() + QLatin1Char('/') + session + QLatin1String(".qws"));
}
/*!
Creates \a session, but does not actually create the file.
*/
bool SessionManager::createSession(const QString &session)
{
if (sessions().contains(session))
return false;
Q_ASSERT(d->m_sessions.size() > 0);
d->m_sessions.insert(1, session);
return true;
}
bool SessionManager::renameSession(const QString &original, const QString &newName)
{
if (!cloneSession(original, newName))
return false;
if (original == activeSession())
loadSession(newName);
return deleteSession(original);
}
/*!
\brief Shows a dialog asking the user to confirm deleting the session \p session
*/
bool SessionManager::confirmSessionDelete(const QString &session)
{
return QMessageBox::question(ICore::mainWindow(),
tr("Delete Session"),
tr("Delete session %1?").arg(session),
QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes;
}
/*!
Deletes \a session name from session list and the file from disk.
*/
bool SessionManager::deleteSession(const QString &session)
{
if (!d->m_sessions.contains(session))
return false;
d->m_sessions.removeOne(session);
QFile fi(sessionNameToFileName(session).toString());
if (fi.exists())
return fi.remove();
return false;
}
bool SessionManager::cloneSession(const QString &original, const QString &clone)
{
if (!d->m_sessions.contains(original))
return false;
QFile fi(sessionNameToFileName(original).toString());
// If the file does not exist, we can still clone
if (!fi.exists() || fi.copy(sessionNameToFileName(clone).toString())) {
Q_ASSERT(d->m_sessions.size() > 0);
d->m_sessions.insert(1, clone);
return true;
}
return false;
}
void SessionManagerPrivate::restoreValues(const PersistentSettingsReader &reader)
{
const QStringList keys = reader.restoreValue(QLatin1String("valueKeys")).toStringList();
foreach (const QString &key, keys) {
QVariant value = reader.restoreValue(QLatin1String("value-") + key);
m_values.insert(key, value);
}
}
void SessionManagerPrivate::restoreDependencies(const PersistentSettingsReader &reader)
{
QMap<QString, QVariant> depMap = reader.restoreValue(QLatin1String("ProjectDependencies")).toMap();
QMap<QString, QVariant>::const_iterator i = depMap.constBegin();
while (i != depMap.constEnd()) {
const QString &key = i.key();
if (!m_failedProjects.contains(key)) {
QStringList values;
foreach (const QString &value, i.value().toStringList()) {
if (!m_failedProjects.contains(value))
values << value;
}
m_depMap.insert(key, values);
}
++i;
}
}
void SessionManagerPrivate::askUserAboutFailedProjects()
{
QStringList failedProjects = m_failedProjects;
if (!failedProjects.isEmpty()) {
QString fileList =
QDir::toNativeSeparators(failedProjects.join(QLatin1String("<br>")));
QMessageBox * box = new QMessageBox(QMessageBox::Warning,
SessionManager::tr("Failed to restore project files"),
SessionManager::tr("Could not restore the following project files:<br><b>%1</b>").
arg(fileList));
QPushButton * keepButton = new QPushButton(SessionManager::tr("Keep projects in Session"), box);
QPushButton * removeButton = new QPushButton(SessionManager::tr("Remove projects from Session"), box);
box->addButton(keepButton, QMessageBox::AcceptRole);
box->addButton(removeButton, QMessageBox::DestructiveRole);
box->exec();
if (box->clickedButton() == removeButton)
m_failedProjects.clear();
}
}
void SessionManagerPrivate::restoreStartupProject(const PersistentSettingsReader &reader)
{
const QString startupProject = reader.restoreValue(QLatin1String("StartupProject")).toString();
if (!startupProject.isEmpty()) {
foreach (Project *pro, d->m_projects) {
if (pro->projectFilePath().toString() == startupProject) {
m_instance->setStartupProject(pro);
break;
}
}
}
if (!m_startupProject) {
if (!startupProject.isEmpty())
qWarning() << "Could not find startup project" << startupProject;
if (!m_projects.isEmpty())
m_instance->setStartupProject(m_projects.first());
}
}
void SessionManagerPrivate::restoreEditors(const PersistentSettingsReader &reader)
{
const QVariant editorsettings = reader.restoreValue(QLatin1String("EditorSettings"));
if (editorsettings.isValid()) {
EditorManager::restoreState(QByteArray::fromBase64(editorsettings.toByteArray()));
sessionLoadingProgress();
}
}
/*!
Loads a session, takes a session name (not filename).
*/
void SessionManagerPrivate::restoreProjects(const QStringList &fileList)
{
// indirectly adds projects to session
// Keep projects that failed to load in the session!
m_failedProjects = fileList;
if (!fileList.isEmpty()) {
ProjectExplorerPlugin::OpenProjectResult result = ProjectExplorerPlugin::openProjects(fileList);
if (!result)
ProjectExplorerPlugin::showOpenProjectError(result);
foreach (Project *p, result.projects())
m_failedProjects.removeAll(p->projectFilePath().toString());
}
}
bool SessionManager::loadSession(const QString &session)
{
// Do nothing if we have that session already loaded,
// exception if the session is the default virgin session
// we still want to be able to load the default session
if (session == d->m_sessionName && !isDefaultVirgin())
return true;
if (!sessions().contains(session))
return false;
QStringList fileList;
// Try loading the file
FileName fileName = sessionNameToFileName(session);
PersistentSettingsReader reader;
if (fileName.exists()) {
if (!reader.load(fileName)) {
QMessageBox::warning(ICore::dialogParent(), tr("Error while restoring session"),
tr("Could not restore session %1").arg(fileName.toUserOutput()));
return false;
}
fileList = reader.restoreValue(QLatin1String("ProjectList")).toStringList();
}
d->m_loadingSession = true;
// Allow everyone to set something in the session and before saving
emit m_instance->aboutToUnloadSession(d->m_sessionName);
if (!isDefaultVirgin()) {
if (!save()) {
d->m_loadingSession = false;
return false;
}
}
// Clean up
if (!EditorManager::closeAllEditors()) {
d->m_loadingSession = false;
return false;
}
setStartupProject(0);
QList<Project *> oldProjects = projects();
auto it = oldProjects.begin();
auto end = oldProjects.end();
while (it != end) {
int index = fileList.indexOf((*it)->projectFilePath().toString());
if (index != -1) {
fileList.removeAt(index);
it = oldProjects.erase(it);
} else {
++it;
}
}
removeProjects(oldProjects);
d->m_failedProjects.clear();
d->m_depMap.clear();
d->m_values.clear();
d->m_casadeSetActive = false;
d->m_sessionName = session;
delete d->m_writer;
d->m_writer = 0;
EditorManager::updateWindowTitles();
if (fileName.exists()) {
d->m_virginSession = false;
ProgressManager::addTask(d->m_future.future(), tr("Loading Session"),
"ProjectExplorer.SessionFile.Load");
d->m_future.setProgressRange(0, 1);
d->m_future.setProgressValue(0);
d->restoreValues(reader);
emit m_instance->aboutToLoadSession(session);
// retrieve all values before the following code could change them again
Id modeId = Id::fromSetting(value(QLatin1String("ActiveMode")));
if (!modeId.isValid())
modeId = Id(Core::Constants::MODE_EDIT);
QColor c = QColor(reader.restoreValue(QLatin1String("Color")).toString());
if (c.isValid())
StyleHelper::setBaseColor(c);
d->m_future.setProgressRange(0, fileList.count() + 1/*initialization above*/ + 1/*editors*/);
d->m_future.setProgressValue(1);
// if one processEvents doesn't get the job done
// just use two!
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
d->restoreProjects(fileList);
d->sessionLoadingProgress();
d->restoreDependencies(reader);
d->restoreStartupProject(reader);
d->restoreEditors(reader);
d->m_future.reportFinished();
d->m_future = QFutureInterface<void>();
// Fall back to Project mode if the startup project is unconfigured and
// use the mode saved in the session otherwise
if (d->m_startupProject && d->m_startupProject->needsConfiguration())
modeId = Id(Constants::MODE_SESSION);
ModeManager::activateMode(modeId);
ModeManager::setFocusToCurrentMode();
} else {
ModeManager::activateMode(Id(Core::Constants::MODE_EDIT));
ModeManager::setFocusToCurrentMode();
}
d->m_casadeSetActive = reader.restoreValue(QLatin1String("CascadeSetActive"), false).toBool();
emit m_instance->sessionLoaded(session);
// Starts a event loop, better do that at the very end
d->askUserAboutFailedProjects();
d->m_loadingSession = false;
return true;
}
QString SessionManager::lastSession()
{
return ICore::settings()->value(QLatin1String("ProjectExplorer/StartupSession")).toString();
}
SessionNode *SessionManager::sessionNode()
{
return d->m_sessionNode;
}
void SessionManager::reportProjectLoadingProgress()
{
d->sessionLoadingProgress();
}
void SessionManager::markSessionFileDirty(bool makeDefaultVirginDirty)
{
if (makeDefaultVirginDirty)
d->m_virginSession = false;
}
void SessionManagerPrivate::sessionLoadingProgress()
{
m_future.setProgressValue(m_future.progressValue() + 1);
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
}
void SessionManager::handleProjectDisplayNameChanged()
{
Project *pro = qobject_cast<Project*>(m_instance->sender());
if (pro) {
d->m_sessionNode->projectDisplayNameChanged(pro->rootProjectNode());
emit m_instance->projectDisplayNameChanged(pro);
}
}
QStringList SessionManager::projectsForSessionName(const QString &session)
{
const FileName fileName = sessionNameToFileName(session);
PersistentSettingsReader reader;
if (fileName.exists()) {
if (!reader.load(fileName)) {
qWarning() << "Could not restore session" << fileName.toUserOutput();
return QStringList();
}
}
return reader.restoreValue(QLatin1String("ProjectList")).toStringList();
}
} // namespace ProjectExplorer
|