source: trunk/UCore.pas

Last change on this file was 19, checked in by chronos, 7 years ago
  • Fixed: Build under Lazarus 1.8.0.
  • Modified: Updated Common package.
File size: 9.5 KB
Line 
1unit UCore;
2
3{$mode delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, XMLConf, LazFileUtils, ActnList, Controls, UVCS, UProject,
9 ULastOpenedList, Forms, Dialogs, Menus, Contnrs, UBackend;
10
11type
12
13 { TCore }
14
15 TCore = class(TDataModule)
16 AAbout: TAction;
17 ACommandUpdate: TAction;
18 ACommandCleanup: TAction;
19 ACommandCommit: TAction;
20 ACommandStatus: TAction;
21 AProjectGroupSaveAs: TAction;
22 AProjectGroupSave: TAction;
23 AProjectGroupClose: TAction;
24 AProjectGroupOpen: TAction;
25 AProjectGroupNew: TAction;
26 AViewTest: TAction;
27 AProjectCheckout: TAction;
28 AProjectClose: TAction;
29 ActionList1: TActionList;
30 AHelp: TAction;
31 AProjectOpen: TAction;
32 AQuit: TAction;
33 ASettings: TAction;
34 AViewFavorites: TAction;
35 ImageList1: TImageList;
36 LastOpenedListProject: TLastOpenedList;
37 LastOpenedListNewDir: TLastOpenedList;
38 LastOpenedListProjectGroup: TLastOpenedList;
39 LastOpenedListRepoURL: TLastOpenedList;
40 OpenDialog1: TOpenDialog;
41 SaveDialog1: TSaveDialog;
42 XMLConfig1: TXMLConfig;
43 procedure ACommandCheckoutExecute(Sender: TObject);
44 procedure ACommandCleanupExecute(Sender: TObject);
45 procedure ACommandCommitExecute(Sender: TObject);
46 procedure ACommandUpdateExecute(Sender: TObject);
47 procedure AProjectCheckoutExecute(Sender: TObject);
48 procedure AProjectCloseExecute(Sender: TObject);
49 procedure AProjectGroupCloseExecute(Sender: TObject);
50 procedure AProjectGroupNewExecute(Sender: TObject);
51 procedure AProjectGroupOpenExecute(Sender: TObject);
52 procedure AProjectGroupSaveAsExecute(Sender: TObject);
53 procedure AProjectGroupSaveExecute(Sender: TObject);
54 procedure AProjectOpenExecute(Sender: TObject);
55 procedure AQuitExecute(Sender: TObject);
56 procedure ASettingsExecute(Sender: TObject);
57 procedure AViewTestExecute(Sender: TObject);
58 procedure DataModuleCreate(Sender: TObject);
59 procedure DataModuleDestroy(Sender: TObject);
60 procedure LastOpenedListProjectChange(Sender: TObject);
61 procedure LastOpenedListProjectGroupChange(Sender: TObject);
62 private
63 procedure UpdateInterface;
64 procedure RegisterBackends;
65 procedure RegisterBackend(Backend: TBackend);
66 function DetectBackend(Directory: string): TBackend;
67 public
68 Backends: TObjectList; // TList<TBackend>
69 Project: TProject;
70 ProjectGroup: TProjectGroup;
71 UserName: string;
72 Email: string;
73 procedure LoadConfig;
74 procedure SaveConfig;
75 procedure Init;
76 procedure Done;
77 procedure ProjectOpen(Directory: string);
78 procedure ProjectGroupOpen(FileName: string);
79 end;
80
81var
82 Core: TCore;
83
84implementation
85
86{$R *.lfm}
87
88uses
89 UFormMain, UFormBrowse, UFormSettings, UFormCommit, UFormCheckout,
90 USubversion, UBazaar, UCVS, UGit, UFormTest, UFormProjectGroup;
91
92{ TCore }
93
94procedure TCore.AQuitExecute(Sender: TObject);
95begin
96 Application.Terminate;
97end;
98
99procedure TCore.ASettingsExecute(Sender: TObject);
100begin
101 FormSettings.Load(XMLConfig1);
102 if FormSettings.ShowModal = mrOk then begin
103 FormSettings.Save(XMLConfig1);
104 end;
105end;
106
107procedure TCore.AViewTestExecute(Sender: TObject);
108begin
109 FormTest.Show;
110end;
111
112procedure TCore.DataModuleCreate(Sender: TObject);
113begin
114 Backends := TObjectList.Create;
115 Project := nil;
116end;
117
118procedure TCore.DataModuleDestroy(Sender: TObject);
119begin
120 FreeAndNil(Project);
121 FreeAndNil(Backends);
122end;
123
124procedure TCore.LastOpenedListProjectChange(Sender: TObject);
125begin
126 LastOpenedListProject.LoadToMenuItem(FormMain.MenuItemOpenRecent, FormMain.OpenRecentExecute);
127end;
128
129procedure TCore.LastOpenedListProjectGroupChange(Sender: TObject);
130begin
131 LastOpenedListProjectGroup.LoadToMenuItem(FormMain.MenuItemRecentProjectGroup, FormMain.ProjectGroupOpenRecentExecute);
132end;
133
134procedure TCore.UpdateInterface;
135begin
136 AProjectClose.Enabled := Assigned(Project);
137 AProjectGroupClose.Enabled := Assigned(ProjectGroup);
138 AProjectGroupSave.Enabled := Assigned(ProjectGroup);
139 AProjectGroupSaveAs.Enabled := Assigned(ProjectGroup);
140 FormMain.UpdateInterface;
141end;
142
143procedure TCore.RegisterBackends;
144begin
145 RegisterBackend(TBackendSubversion.Create);
146 RegisterBackend(TBackendBazaar.Create);
147 RegisterBackend(TBackendCVS.Create);
148 RegisterBackend(TBackendGit.Create);
149end;
150
151procedure TCore.RegisterBackend(Backend: TBackend);
152begin
153 Backends.Add(Backend);
154end;
155
156function TCore.DetectBackend(Directory: string): TBackend;
157var
158 I: Integer;
159begin
160 I := 0;
161 while (I < Backends.Count) and (not TBackend(Backends[I]).IsWorkingCopy(Directory)) do Inc(I);
162 if I < Backends.Count then Result := TBackend(Backends[I])
163 else Result := nil;
164end;
165
166procedure TCore.LoadConfig;
167begin
168 LastOpenedListProject.LoadFromXMLConfig(XMLConfig1, 'LastOpenedProjects');
169 LastOpenedListRepoURL.LoadFromXMLConfig(XMLConfig1, 'LastOpenedRepoURL');
170 LastOpenedListNewDir.LoadFromXMLConfig(XMLConfig1, 'LastOpenedNewDir');
171 LastOpenedListProjectGroup.LoadFromXMLConfig(XMLConfig1, 'LastOpenedListProjectGroup');
172 UserName := XMLConfig1.GetValue('UserName', '');
173 Email := XMLConfig1.GetValue('Email', '');
174end;
175
176procedure TCore.SaveConfig;
177begin
178 LastOpenedListProject.SaveToXMLConfig(XMLConfig1, 'LastOpenedProjects');
179 LastOpenedListRepoURL.SaveToXMLConfig(XMLConfig1, 'LastOpenedRepoURL');
180 LastOpenedListNewDir.SaveToXMLConfig(XMLConfig1, 'LastOpenedNewDir');
181 LastOpenedListProjectGroup.SaveToXMLConfig(XMLConfig1, 'LastOpenedListProjectGroup');
182 XMLConfig1.SetValue('UserName', UserName);
183 XMLConfig1.SetValue('Email', Email);
184 XmlConfig1.Flush;
185end;
186
187procedure TCore.Init;
188begin
189 RegisterBackends;
190 XMLConfig1.Filename := GetAppConfigDir(False) + 'Config.xml';
191 ForceDirectoriesUTF8(ExtractFileDir(XMLConfig1.Filename));
192 LoadConfig;
193 if (LastOpenedListProject.Items.Count > 0) and DirectoryExistsUTF8(LastOpenedListProject.Items[0]) then
194 ProjectOpen(LastOpenedListProject.Items[0]);
195end;
196
197procedure TCore.Done;
198begin
199 SaveConfig;
200end;
201
202procedure TCore.ProjectOpen(Directory: string);
203begin
204 AProjectClose.Execute;
205 Project := TProject.Create;
206 Project.Backend := DetectBackend(Directory);
207 Project.WorkingCopy.UserName := Core.UserName;
208 Project.WorkingCopy.Email := Core.Email;
209 if Assigned(Project.Backend) then begin
210 Project.Directory := Directory;
211 UpdateInterface;
212 FormBrowse.Directory := Project.Directory;
213 FormBrowse.ReloadList;
214 LastOpenedListProject.AddItem(Project.Directory);
215 end else ShowMessage('Directory ''' + Directory + ''' not recognized as working copy of any of supported VCS systems');
216end;
217
218procedure TCore.ProjectGroupOpen(FileName: string);
219begin
220 AProjectGroupClose.Execute;
221 ProjectGroup := TProjectGroup.Create;
222 ProjectGroup.FileName := FileName;
223 UpdateInterface;
224 FormProjectGroup.ReloadTree;
225 LastOpenedListProjectGroup.AddItem(FileName);
226end;
227
228procedure TCore.AProjectOpenExecute(Sender: TObject);
229var
230 Dir: string;
231 OldDir: string;
232begin
233 if Assigned(Project) then OldDir := Project.Directory
234 else OldDir := '';
235 if SelectDirectory('Open project', OldDir, Dir) then begin
236 ProjectOpen(Dir);
237 end;
238end;
239
240procedure TCore.AProjectCloseExecute(Sender: TObject);
241begin
242 FreeAndNil(Project);
243 FormBrowse.Directory := '';
244 FormBrowse.ReloadList;
245 UpdateInterface;
246end;
247
248procedure TCore.AProjectGroupCloseExecute(Sender: TObject);
249begin
250 FreeAndNil(ProjectGroup);
251 UpdateInterface;
252 FormProjectGroup.ReloadTree;
253end;
254
255procedure TCore.AProjectGroupNewExecute(Sender: TObject);
256begin
257 AProjectClose.Execute;
258 ProjectGroup := TProjectGroup.Create;
259 ProjectGroup.FileName := 'New project group.vcgrp';
260 UpdateInterface;
261 FormProjectGroup.ReloadTree;
262end;
263
264procedure TCore.AProjectGroupOpenExecute(Sender: TObject);
265begin
266 if LastOpenedListProjectGroup.Items.Count > 0 then
267 OpenDialog1.FileName := LastOpenedListProjectGroup.Items[0];
268 if OpenDialog1.Execute then begin
269 AProjectClose.Execute;
270 ProjectGroupOpen(OpenDialog1.FileName);
271 end;
272end;
273
274procedure TCore.AProjectGroupSaveAsExecute(Sender: TObject);
275begin
276 SaveDialog1.FileName := ProjectGroup.FileName;
277 if SaveDialog1.Execute then begin
278 ProjectGroup.SaveToFile(SaveDialog1.FileName);
279 UpdateInterface;
280 end;
281end;
282
283procedure TCore.AProjectGroupSaveExecute(Sender: TObject);
284begin
285 if FileExistsUTF8(ProjectGroup.FileName) then ProjectGroup.SaveToFile(ProjectGroup.FileName)
286 else AProjectGroupSaveAs.Execute;
287end;
288
289procedure TCore.ACommandUpdateExecute(Sender: TObject);
290begin
291 Project.WorkingCopy.Update;
292end;
293
294procedure TCore.AProjectCheckoutExecute(Sender: TObject);
295var
296 TempProject: TProject;
297begin
298 if LastOpenedListRepoURL.Items.Count > 0 then
299 FormCheckout.EditURL.Text := LastOpenedListRepoURL.Items[0];
300 if LastOpenedListNewDir.Items.Count > 0 then
301 FormCheckout.EditDir.Text := LastOpenedListNewDir.Items[0];
302 if FormCheckout.ShowModal = mrOk then begin
303 LastOpenedListRepoURL.AddItem(FormCheckout.EditURL.Text);
304 LastOpenedListNewDir.AddItem(FormCheckout.EditDir.Text);
305 TempProject := TProject.Create;
306 try
307 TempProject.Backend := TBackend(FormCheckout.ComboBox1.Items.Objects[FormCheckout.ComboBox1.ItemIndex]);
308 TempProject.Directory := FormCheckout.EditDir.Text;
309 TempProject.RepositoryURL := FormCheckout.EditURL.Text;
310 TempProject.WorkingCopy.Checkout;
311 finally
312 TempProject.Free;
313 end;
314 ProjectOpen(FormCheckout.EditDir.Text);
315 end;
316end;
317
318procedure TCore.ACommandCleanupExecute(Sender: TObject);
319begin
320 Project.WorkingCopy.Cleanup;
321end;
322
323procedure TCore.ACommandCheckoutExecute(Sender: TObject);
324begin
325
326end;
327
328procedure TCore.ACommandCommitExecute(Sender: TObject);
329begin
330 if FormCommit.ShowModal = mrOk then begin
331 Project.WorkingCopy.Commit(FormCommit.MemoMessage.Lines);
332 end;
333end;
334
335end.
336
Note: See TracBrowser for help on using the repository browser.