Updating linux build.
[Widgit.git] / FileUpdater.cs
blob3264e28dca4a9f5244cb5db2fafb5dabd4b489f6
1 //Widgit is free software; you can redistribute it and/or modify
2 //it under the terms of the GNU General Public License as published by
3 //the Free Software Foundation; either version 2 of the License, or
4 //(at your option) any later version.
6 //Widgit is distributed in the hope that it will be useful,
7 //but WITHOUT ANY WARRANTY; without even the implied warranty of
8 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 //GNU General Public License for more details.
11 //You should have received a copy of the GNU General Public License
12 //along with Widgit; if not, write to the Free Software
13 //Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USAusing System;
15 using System;
16 using System.Collections.Generic;
17 using System.Text;
18 using Git;
20 namespace Widgit
22 using FilesList = List<Git.File>;
24 public class FilesUpdatedEvent : EventArgs
26 public FilesUpdatedEvent(FilesList files)
28 m_files = new FilesList(files);
30 protected FilesList m_files;
31 public FilesList FilesList { get { return new FilesList(m_files); } }
34 public delegate void FileUpdatedHandler(FileUpdater sender, FilesUpdatedEvent evt);
36 public class FileUpdater
38 public event FileUpdatedHandler FilesUpdated;
40 Prefs m_prefs;
42 protected Dictionary<IFileOperations, ISelectable> m_EventSourcesToSelectables = new Dictionary<IFileOperations,ISelectable>();
43 public ISelectable this[IFileOperations o]
45 get { return m_EventSourcesToSelectables[o]; }
46 set
48 if (null == value)
50 o.Add -= OnAddFiles;
51 o.Copy -= OnCopyFiles;
52 o.Delete -= OnDeleteFiles;
53 o.RenameOrMove -= OnMoveFiles;
54 o.Revert -= OnRevertFiles;
55 o.DiffHead -= OnDiffFile;
56 o.DiffOther -= OnDiffHistoryFile;
57 o.View -= OnViewFile;
58 o.ViewPrevious -= OnViewPreviousFile;
59 m_EventSourcesToSelectables.Remove(o);
61 else
63 m_EventSourcesToSelectables[o] = value;
64 o.Add += OnAddFiles;
65 o.Copy += OnCopyFiles;
66 o.Delete += OnDeleteFiles;
67 o.RenameOrMove += OnMoveFiles;
68 o.Revert += OnRevertFiles;
69 o.DiffHead += OnDiffFile;
70 o.DiffOther += OnDiffHistoryFile;
71 o.View += OnViewFile;
72 o.ViewPrevious += OnViewPreviousFile;
78 Repo m_currentRepo;
80 internal FileUpdater(Prefs prefs)
82 m_prefs = prefs;
85 public void AddEventSource(IFileOperations f, ISelectable s)
87 m_EventSourcesToSelectables[f] = s;
88 f.Add += OnAddFiles;
89 f.Copy += OnCopyFiles;
90 f.Delete += OnDeleteFiles;
91 f.RenameOrMove += OnMoveFiles;
92 f.Revert += OnRevertFiles;
95 public void SetRepo(Repo repo)
97 m_currentRepo = repo;
100 public bool AddFiles(FilesList files)
102 List<string> paths = MakePathsList(files);
103 FilesList updatedFiles;
104 if (m_currentRepo.Add(paths, out updatedFiles) && files.Count > 0)
106 OnFileUpdated(new FilesUpdatedEvent(updatedFiles));
108 return true;
111 public bool DeleteFiles(FilesList files)
113 List<string> paths = MakePathsList(files);
114 FilesList updatedFiles;
115 if (m_currentRepo.Remove(paths, out updatedFiles) && files.Count > 0)
117 OnFileUpdated(new FilesUpdatedEvent(updatedFiles));
119 return true;
122 public bool RevertFiles(FilesList files)
124 List<string> paths = MakePathsList(files);
125 FilesList updatedFiles;
126 if (m_currentRepo.Revert(paths, out updatedFiles) && files.Count > 0)
128 OnFileUpdated(new FilesUpdatedEvent(updatedFiles));
130 return true;
133 private static List<string> MakePathsList(FilesList files)
135 List<string> paths = new List<string>();
136 foreach (File f in files)
138 if (f.RelativePath.Length > 0)
140 paths.Add(f.PathWithName);
142 else
144 paths.Add(f.Name);
147 return paths;
150 protected void OnFileUpdated(FilesUpdatedEvent e)
152 if (FilesUpdated != null)
154 FilesUpdated(this, e);
158 protected void OnAddFiles(object sender, EventArgs e)
160 ISelectable s = m_EventSourcesToSelectables[(IFileOperations)sender];
161 AddFiles(s.GetSelected());
164 protected void OnDeleteFiles(object sender, EventArgs e)
166 ISelectable s = m_EventSourcesToSelectables[(IFileOperations)sender];
167 DeleteFiles (s.GetSelected());
170 protected void OnRevertFiles(object sender, EventArgs e)
172 ISelectable s = m_EventSourcesToSelectables[(IFileOperations)sender];
173 RevertFiles(s.GetSelected());
176 protected void OnViewFile(object sender, EventArgs e)
178 ISelectable s = m_EventSourcesToSelectables[(IFileOperations)sender];
179 foreach (File f in s.GetSelected())
181 string path = SerializeRevision(f, null);
183 if (String.IsNullOrEmpty(m_prefs.ViewerApp))
185 System.Diagnostics.ProcessStartInfo si = new System.Diagnostics.ProcessStartInfo();
186 si.FileName = path;
187 si.UseShellExecute = true;
188 System.Console.WriteLine(si.FileName + " " + si.Arguments);
189 System.Diagnostics.Process p = new System.Diagnostics.Process();
190 p.StartInfo = si;
191 p.Start();
193 else
195 LaunchApp(m_prefs.ViewerApp, m_prefs.ViewerArgs, path, "", "", "");
200 protected void OnViewPreviousFile(object sender, EventArgs e)
202 ISelectable s = m_EventSourcesToSelectables[(IFileOperations)sender];
203 RevertFiles(s.GetSelected());
206 protected void OnDiffFile(object sender, EventArgs e)
208 ISelectable s = m_EventSourcesToSelectables[(IFileOperations)sender];
209 if (String.IsNullOrEmpty(m_prefs.DiffApp))
211 return;
213 foreach (File f in s.GetSelected())
215 Diff(f, null, f, "HEAD");
219 protected void OnDiffHistoryFile(object sender, EventArgs e)
221 ISelectable s = m_EventSourcesToSelectables[(IFileOperations)sender];
222 if (String.IsNullOrEmpty(m_prefs.DiffApp))
224 return;
227 List<Git.File> files = s.GetSelected();
228 if (files.Count < 0)
230 return;
232 List<string> paths = new List<string>();
233 foreach (File f in files)
235 paths.Add(f.PathWithName);
237 RevisionChooser c = new RevisionChooser();
238 string commitish = c.Choose(m_currentRepo, paths);
239 if (null == commitish)
241 return;
243 foreach (File f in files)
245 Diff(f, null, f, commitish);
250 private string ReplaceOrAppend(string orig, string toReplace, string replaceWith)
252 if (orig.IndexOf(toReplace) < 0)
254 orig += " \"" + replaceWith + "\"";
256 else
258 orig = orig.Replace("[File]", "\"" + replaceWith + "\"");
260 return orig;
263 protected string SerializeRevision(File f, string commitish)
265 string contents;
266 if (String.IsNullOrEmpty(commitish))
268 string nativePath = f.PathWithName.Replace(Repo.DirectorySeparator,
269 System.IO.Path.DirectorySeparatorChar.ToString());
270 nativePath = m_currentRepo.Path + System.IO.Path.DirectorySeparatorChar + nativePath;
271 return nativePath;
273 if (!f.GetRevision(commitish, out contents))
275 return null;
277 string tmpFileName = System.IO.Path.GetTempFileName();
278 System.IO.StreamWriter writer = new System.IO.StreamWriter(tmpFileName);
279 writer.Write(contents);
280 writer.Close();
281 return tmpFileName;
284 protected bool Diff(File file1, string commitish1, File file2, string commitish2)
286 if (null == file2)
288 file2 = file1;
291 string nativePath1 = SerializeRevision(file1, commitish1);
292 if (null == nativePath1)
294 return false;
297 string nativePath2 = SerializeRevision(file2, commitish2);
298 if (null == nativePath2)
300 return false;
302 return LaunchApp(m_prefs.DiffApp, m_prefs.DiffArgs, "", nativePath2, nativePath1, "");
305 protected bool LaunchApp(string execPath, string args, string file, string yours, string theirs, string target)
307 System.Diagnostics.ProcessStartInfo si = new System.Diagnostics.ProcessStartInfo();
308 si.FileName = execPath;
309 args = ReplaceOrAppend(args, "[File]", file);
310 args = ReplaceOrAppend(args, "[Theirs]", theirs);
311 args = ReplaceOrAppend(args, "[Yours]", yours);
312 args = ReplaceOrAppend(args, "[Target]", target);
313 si.Arguments = args;
314 System.Console.WriteLine(si.FileName + " " + si.Arguments);
315 System.Diagnostics.Process p = new System.Diagnostics.Process();
316 p.StartInfo = si;
317 p.Start();
318 return true;
321 protected void OnCopyFiles(object sender, EventArgs e)
323 //ISelectable s = m_EventSourcesToSelectables[sender];
324 //CopyFiles(s.GetSelected());
327 protected void OnMoveFiles(object sender, EventArgs e)
329 //ISelectable s = m_EventSourcesToSelectables[sender];
330 //RevertFiles(s.GetSelected()); ;