2010-07-04 10:39:16 +00:00
|
|
|
|
// SparkleShare, an instant update workflow to Git.
|
|
|
|
|
// Copyright (C) 2010 Hylke Bons <hylkebons@gmail.com>
|
|
|
|
|
//
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
using Gtk;
|
|
|
|
|
using Mono.Unix;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
2010-07-13 18:23:59 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
2010-07-04 10:39:16 +00:00
|
|
|
|
namespace SparkleShare {
|
|
|
|
|
|
2010-07-06 07:55:06 +00:00
|
|
|
|
// The main window for SparkleDiff
|
2010-07-04 10:39:16 +00:00
|
|
|
|
public class SparkleDiffWindow : Window
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
// Short alias for the translations
|
|
|
|
|
public static string _ (string s)
|
|
|
|
|
{
|
|
|
|
|
return Catalog.GetString (s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private RevisionView ViewLeft;
|
|
|
|
|
private RevisionView ViewRight;
|
|
|
|
|
|
|
|
|
|
private string [] Revisions;
|
|
|
|
|
|
2010-07-06 07:51:37 +00:00
|
|
|
|
public SparkleDiffWindow (string file_path, string [] revisions) : base ("")
|
2010-07-04 10:39:16 +00:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
string file_name = System.IO.Path.GetFileName (file_path);
|
2010-07-06 07:51:37 +00:00
|
|
|
|
Revisions = revisions;
|
2010-07-04 10:39:16 +00:00
|
|
|
|
|
|
|
|
|
SetPosition (WindowPosition.Center);
|
|
|
|
|
BorderWidth = 12;
|
2010-07-06 07:51:37 +00:00
|
|
|
|
IconName = "image-x-generic";
|
2010-07-04 10:39:16 +00:00
|
|
|
|
|
|
|
|
|
DeleteEvent += Quit;
|
|
|
|
|
|
|
|
|
|
// TRANSLATORS: The parameter is a filename
|
|
|
|
|
Title = String.Format(_("Comparing Revisions of ‘{0}’"), file_name);
|
|
|
|
|
|
|
|
|
|
VBox layout_vertical = new VBox (false, 12);
|
|
|
|
|
|
2010-07-13 18:23:59 +00:00
|
|
|
|
HBox layout_horizontal = new HBox (true, 6);
|
2010-07-04 10:39:16 +00:00
|
|
|
|
|
|
|
|
|
Process process = new Process ();
|
|
|
|
|
process.EnableRaisingEvents = true;
|
|
|
|
|
process.StartInfo.RedirectStandardOutput = true;
|
|
|
|
|
process.StartInfo.UseShellExecute = false;
|
|
|
|
|
|
|
|
|
|
process.StartInfo.WorkingDirectory = System.IO.Path.GetDirectoryName (file_path);
|
|
|
|
|
process.StartInfo.FileName = "git";
|
2010-07-13 18:23:59 +00:00
|
|
|
|
process.StartInfo.Arguments = "log --format=\"%ct\t%an\t%ae\" " + file_name;
|
2010-07-04 10:39:16 +00:00
|
|
|
|
process.Start ();
|
|
|
|
|
|
2010-07-13 18:23:59 +00:00
|
|
|
|
ViewLeft = new LeftRevisionView ();
|
|
|
|
|
ViewRight = new RightRevisionView ();
|
|
|
|
|
|
2010-07-04 10:39:16 +00:00
|
|
|
|
string output = process.StandardOutput.ReadToEnd ();
|
|
|
|
|
string [] revisions_info = Regex.Split (output.Trim (), "\n");
|
|
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
foreach (string revision_info in revisions_info) {
|
|
|
|
|
|
|
|
|
|
string [] parts = Regex.Split (revision_info.Trim (), "\t");
|
|
|
|
|
|
|
|
|
|
int timestamp = int.Parse (parts [0]);
|
|
|
|
|
string author = parts [1];
|
2010-07-13 18:23:59 +00:00
|
|
|
|
string email = parts [2];
|
2010-07-04 10:39:16 +00:00
|
|
|
|
|
2010-07-13 18:23:59 +00:00
|
|
|
|
string date;
|
|
|
|
|
// TRANSLATORS: This is a format specifier according to System.Globalization.DateTimeFormatInfo
|
2010-07-04 10:39:16 +00:00
|
|
|
|
if (i == 0)
|
2010-07-13 18:23:59 +00:00
|
|
|
|
date = "Latest Revision";
|
2010-07-04 10:39:16 +00:00
|
|
|
|
else
|
2010-07-13 18:23:59 +00:00
|
|
|
|
date = String.Format (_("{0} at {1}"),
|
|
|
|
|
UnixTimestampToDateTime (timestamp).ToString (_("ddd MMM d, yyyy")),
|
|
|
|
|
UnixTimestampToDateTime (timestamp).ToString (_("H:mm")));
|
2010-07-04 10:39:16 +00:00
|
|
|
|
|
2010-07-13 18:23:59 +00:00
|
|
|
|
ViewLeft.AddRow (GetAvatar (email, 32), author, date);
|
|
|
|
|
ViewRight.AddRow (GetAvatar (email, 32), author, date);
|
|
|
|
|
|
2010-07-04 10:39:16 +00:00
|
|
|
|
i++;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-07-04 23:15:25 +00:00
|
|
|
|
ViewLeft.SetImage (new RevisionImage (file_path, Revisions [1]));
|
2010-07-04 10:39:16 +00:00
|
|
|
|
ViewRight.SetImage (new RevisionImage (file_path, Revisions [0]));
|
2010-07-07 17:03:23 +00:00
|
|
|
|
|
2010-07-13 18:23:59 +00:00
|
|
|
|
ViewLeft.IconView.SelectionChanged += delegate {
|
2010-07-07 17:03:23 +00:00
|
|
|
|
|
2010-07-13 18:23:59 +00:00
|
|
|
|
ViewLeft.SetImage (new RevisionImage (file_path, Revisions [ViewLeft.GetSelected ()]));
|
2010-07-04 10:39:16 +00:00
|
|
|
|
|
2010-07-13 18:23:59 +00:00
|
|
|
|
ViewLeft.ScrolledWindow.Hadjustment = ViewRight.ScrolledWindow.Hadjustment;
|
|
|
|
|
ViewLeft.ScrolledWindow.Vadjustment = ViewRight.ScrolledWindow.Vadjustment;
|
|
|
|
|
|
|
|
|
|
HookUpViews ();
|
2010-07-04 10:39:16 +00:00
|
|
|
|
|
2010-07-13 18:23:59 +00:00
|
|
|
|
};
|
2010-07-04 10:39:16 +00:00
|
|
|
|
|
2010-07-13 18:23:59 +00:00
|
|
|
|
ViewRight.IconView.SelectionChanged += delegate {
|
|
|
|
|
|
|
|
|
|
ViewRight.SetImage (new RevisionImage (file_path, Revisions [ViewRight.GetSelected ()]));
|
2010-07-04 10:39:16 +00:00
|
|
|
|
|
2010-07-13 18:23:59 +00:00
|
|
|
|
ViewRight.ScrolledWindow.Hadjustment = ViewLeft.ScrolledWindow.Hadjustment;
|
|
|
|
|
ViewRight.ScrolledWindow.Vadjustment = ViewLeft.ScrolledWindow.Vadjustment;
|
|
|
|
|
|
|
|
|
|
HookUpViews ();
|
2010-07-04 10:39:16 +00:00
|
|
|
|
|
2010-07-13 18:23:59 +00:00
|
|
|
|
};
|
2010-07-07 17:03:23 +00:00
|
|
|
|
|
2010-07-13 18:23:59 +00:00
|
|
|
|
layout_horizontal.PackStart (ViewLeft);
|
|
|
|
|
layout_horizontal.PackStart (ViewRight);
|
2010-07-07 17:03:23 +00:00
|
|
|
|
|
2010-07-04 10:39:16 +00:00
|
|
|
|
|
|
|
|
|
|
2010-07-05 13:15:22 +00:00
|
|
|
|
ResizeToViews ();
|
|
|
|
|
|
2010-07-04 10:39:16 +00:00
|
|
|
|
// Order time view according to the user's reading direction
|
2010-07-14 15:28:33 +00:00
|
|
|
|
if (Direction == Gtk.TextDirection.Rtl)
|
2010-07-04 10:39:16 +00:00
|
|
|
|
layout_horizontal.ReorderChild (ViewLeft, 1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HookUpViews ();
|
|
|
|
|
|
|
|
|
|
HButtonBox dialog_buttons = new HButtonBox ();
|
|
|
|
|
dialog_buttons.Layout = ButtonBoxStyle.End;
|
|
|
|
|
dialog_buttons.BorderWidth = 0;
|
|
|
|
|
|
2010-07-14 15:28:33 +00:00
|
|
|
|
Button close_button = new Button (Stock.Close);
|
|
|
|
|
close_button.Clicked += delegate (object o, EventArgs args) {
|
2010-07-04 10:39:16 +00:00
|
|
|
|
Environment.Exit (0);
|
|
|
|
|
};
|
2010-07-14 15:28:33 +00:00
|
|
|
|
|
|
|
|
|
dialog_buttons.Add (close_button);
|
2010-07-04 10:39:16 +00:00
|
|
|
|
|
|
|
|
|
layout_vertical.PackStart (layout_horizontal, true, true, 0);
|
|
|
|
|
layout_vertical.PackStart (dialog_buttons, false, false, 0);
|
|
|
|
|
|
|
|
|
|
Add (layout_vertical);
|
|
|
|
|
|
|
|
|
|
}
|
2010-07-13 18:23:59 +00:00
|
|
|
|
|
|
|
|
|
// Converts a UNIX timestamp to a more usable time object
|
|
|
|
|
public DateTime UnixTimestampToDateTime (int timestamp)
|
|
|
|
|
{
|
|
|
|
|
DateTime unix_epoch = new DateTime (1970, 1, 1, 0, 0, 0, 0);
|
|
|
|
|
return unix_epoch.AddSeconds (timestamp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Looks up an icon from the system's theme
|
|
|
|
|
public Gdk.Pixbuf GetIcon (string name, int size)
|
|
|
|
|
{
|
|
|
|
|
IconTheme icon_theme = new IconTheme ();
|
|
|
|
|
icon_theme.AppendSearchPath (System.IO.Path.Combine ("/usr/share/sparkleshare", "icons"));
|
|
|
|
|
return icon_theme.LoadIcon (name, size, IconLookupFlags.GenericFallback);
|
|
|
|
|
}
|
2010-07-04 10:39:16 +00:00
|
|
|
|
|
|
|
|
|
|
2010-07-06 07:51:37 +00:00
|
|
|
|
private void ResizeToViews ()
|
|
|
|
|
{
|
2010-07-05 13:15:22 +00:00
|
|
|
|
|
2010-07-07 22:55:50 +00:00
|
|
|
|
int new_width = ViewLeft.GetImage ().Pixbuf.Width + ViewRight.GetImage ().Pixbuf.Width + 200;
|
2010-07-05 13:15:22 +00:00
|
|
|
|
int new_height = 200;
|
|
|
|
|
|
|
|
|
|
if (ViewLeft.GetImage ().Pixbuf.Height > ViewRight.GetImage ().Pixbuf.Height)
|
|
|
|
|
new_height += ViewLeft.GetImage ().Pixbuf.Height;
|
|
|
|
|
else
|
|
|
|
|
new_height += ViewRight.GetImage ().Pixbuf.Height;
|
|
|
|
|
|
|
|
|
|
if (new_width >= Screen.Width || new_height >= Screen.Height)
|
|
|
|
|
Maximize ();
|
|
|
|
|
else
|
|
|
|
|
SetSizeRequest (new_width, new_height);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-04 10:39:16 +00:00
|
|
|
|
// Hooks up two views so their scrollbars will be kept in sync
|
2010-07-06 07:51:37 +00:00
|
|
|
|
private void HookUpViews ()
|
|
|
|
|
{
|
2010-07-04 10:39:16 +00:00
|
|
|
|
|
|
|
|
|
ViewLeft.ScrolledWindow.Hadjustment.ValueChanged += SyncViewsHorizontally;
|
|
|
|
|
ViewLeft.ScrolledWindow.Vadjustment.ValueChanged += SyncViewsVertically;
|
|
|
|
|
ViewRight.ScrolledWindow.Hadjustment.ValueChanged += SyncViewsHorizontally;
|
|
|
|
|
ViewRight.ScrolledWindow.Vadjustment.ValueChanged += SyncViewsVertically;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Keeps the two image views in sync horizontally
|
2010-07-06 07:51:37 +00:00
|
|
|
|
private void SyncViewsHorizontally (object o, EventArgs args)
|
|
|
|
|
{
|
2010-07-04 10:39:16 +00:00
|
|
|
|
|
|
|
|
|
Adjustment source_adjustment = (Adjustment) o;
|
|
|
|
|
|
|
|
|
|
if (source_adjustment == ViewLeft.ScrolledWindow.Hadjustment)
|
|
|
|
|
ViewRight.ScrolledWindow.Hadjustment = source_adjustment;
|
|
|
|
|
else
|
|
|
|
|
ViewLeft.ScrolledWindow.Hadjustment = source_adjustment;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Keeps the two image views in sync vertically
|
2010-07-06 07:51:37 +00:00
|
|
|
|
private void SyncViewsVertically (object o, EventArgs args)
|
|
|
|
|
{
|
2010-07-04 10:39:16 +00:00
|
|
|
|
|
|
|
|
|
Adjustment source_adjustment = (Adjustment) o;
|
|
|
|
|
|
|
|
|
|
if (source_adjustment == ViewLeft.ScrolledWindow.Vadjustment)
|
|
|
|
|
ViewRight.ScrolledWindow.Vadjustment = source_adjustment;
|
|
|
|
|
else
|
|
|
|
|
ViewLeft.ScrolledWindow.Vadjustment = source_adjustment;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-07-13 18:23:59 +00:00
|
|
|
|
public string CombineMore (params string [] Parts)
|
2010-07-04 10:39:16 +00:00
|
|
|
|
{
|
2010-07-13 18:23:59 +00:00
|
|
|
|
string NewPath = " ";
|
|
|
|
|
foreach (string Part in Parts)
|
|
|
|
|
NewPath = System.IO.Path.Combine (NewPath, Part);
|
|
|
|
|
return NewPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Creates an MD5 hash of input
|
|
|
|
|
public static string GetMD5 (string s)
|
|
|
|
|
{
|
|
|
|
|
MD5 md5 = new MD5CryptoServiceProvider ();
|
|
|
|
|
Byte[] bytes = ASCIIEncoding.Default.GetBytes (s);
|
|
|
|
|
Byte[] encodedBytes = md5.ComputeHash (bytes);
|
|
|
|
|
return BitConverter.ToString (encodedBytes).ToLower ().Replace ("-", "");
|
2010-07-04 10:39:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-07-13 18:23:59 +00:00
|
|
|
|
// TODO: Turn this into an avatar fetching library
|
|
|
|
|
// TODO: This should be included from SparkleHelpers, but I don't know how to do that
|
|
|
|
|
// Gets the avatar for a specific email address and size
|
|
|
|
|
public Gdk.Pixbuf GetAvatar (string Email, int Size)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
UnixUserInfo UnixUserInfo = new UnixUserInfo (UnixEnvironment.UserName);
|
|
|
|
|
|
|
|
|
|
string HomePath = UnixUserInfo.HomeDirectory;
|
|
|
|
|
|
2010-07-13 19:04:45 +00:00
|
|
|
|
string SparkleLocalIconPath = CombineMore (HomePath, ".icons", "sparkleshare");
|
2010-07-13 18:23:59 +00:00
|
|
|
|
|
|
|
|
|
string AvatarPath = CombineMore (SparkleLocalIconPath, Size + "x" + Size, "status");
|
|
|
|
|
|
|
|
|
|
if (!Directory.Exists (AvatarPath)) {
|
|
|
|
|
Directory.CreateDirectory (AvatarPath);
|
|
|
|
|
// SparkleHelpers.DebugInfo ("Config", "Created '" + AvatarPath + "'");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string AvatarFilePath = CombineMore (AvatarPath, Email);
|
|
|
|
|
|
|
|
|
|
if (File.Exists (AvatarFilePath))
|
|
|
|
|
return new Gdk.Pixbuf (AvatarFilePath);
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
|
|
// Let's try to get the person's gravatar for next time
|
|
|
|
|
WebClient WebClient = new WebClient ();
|
|
|
|
|
Uri GravatarUri = new Uri ("http://www.gravatar.com/avatar/" + GetMD5 (Email) +
|
|
|
|
|
".jpg?s=" + Size + "&d=404");
|
|
|
|
|
|
2010-07-13 19:04:45 +00:00
|
|
|
|
string TmpFile = CombineMore (HomePath, "SparkleShare", ".tmp", Email + Size);
|
2010-07-13 18:23:59 +00:00
|
|
|
|
|
|
|
|
|
if (!File.Exists (TmpFile)) {
|
|
|
|
|
|
|
|
|
|
WebClient.DownloadFileAsync (GravatarUri, TmpFile);
|
|
|
|
|
WebClient.DownloadFileCompleted += delegate {
|
2010-07-13 19:04:45 +00:00
|
|
|
|
File.Delete (AvatarFilePath);
|
2010-07-13 18:23:59 +00:00
|
|
|
|
FileInfo TmpFileInfo = new FileInfo (TmpFile);
|
|
|
|
|
if (TmpFileInfo.Length > 255)
|
|
|
|
|
File.Move (TmpFile, AvatarFilePath);
|
2010-07-13 19:04:45 +00:00
|
|
|
|
Console.WriteLine ("AAAAAAA");
|
2010-07-13 18:23:59 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fall back to a generic icon if there is no gravatar
|
|
|
|
|
if (File.Exists (AvatarFilePath))
|
|
|
|
|
return new Gdk.Pixbuf (AvatarFilePath);
|
|
|
|
|
else
|
|
|
|
|
return GetIcon ("avatar-default", Size);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-04 10:39:16 +00:00
|
|
|
|
// Quits the program
|
2010-07-06 07:51:37 +00:00
|
|
|
|
private void Quit (object o, EventArgs args)
|
|
|
|
|
{
|
2010-07-04 10:39:16 +00:00
|
|
|
|
Environment.Exit (0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|