SparkleShare/SparkleDiff/SparkleDiff.cs

416 lines
10 KiB
C#
Raw Normal View History

2010-06-28 12:17:48 +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.IO;
using System.Text.RegularExpressions;
namespace SparkleShare {
public class SparkleDiff
{
public static void Main (string [] args)
{
2010-06-30 00:08:02 +00:00
if (args.Length > 0) {
string file_path = args [0];
if (File.Exists (file_path)) {
Gtk.Application.Init ();
SparkleDiffWindow sparkle_diff_window;
sparkle_diff_window = new SparkleDiffWindow (file_path);
sparkle_diff_window.ShowAll ();
// The main loop
Gtk.Application.Run ();
2010-06-28 12:17:48 +00:00
2010-06-30 00:08:02 +00:00
} else {
Console.WriteLine ("SparkleDiff: " + file_path + ": No such file or directory.");
Environment.Exit (0);
}
}
2010-06-28 12:17:48 +00:00
}
}
public class SparkleDiffWindow : Window
{
// Short alias for the translations
public static string _ (string s)
{
return Catalog.GetString (s);
}
2010-06-29 10:20:58 +00:00
private RevisionView ViewLeft;
private RevisionView ViewRight;
2010-06-28 12:17:48 +00:00
2010-06-30 00:08:02 +00:00
private string [] Revisions;
2010-06-28 12:17:48 +00:00
public SparkleDiffWindow (string file_path) : base ("")
{
2010-06-30 00:08:02 +00:00
string file_name = System.IO.Path.GetFileName (file_path);
2010-06-28 12:17:48 +00:00
SetSizeRequest (800, 540);
2010-06-28 12:17:48 +00:00
SetPosition (WindowPosition.Center);
BorderWidth = 12;
2010-06-30 00:08:02 +00:00
DeleteEvent += Quit;
IconName = "image-x-generic";
Title = String.Format(_("Comparing Revisions of {0}"), file_name);
2010-06-28 12:17:48 +00:00
2010-06-30 00:08:02 +00:00
Revisions = GetRevisionsForFile (file_path);
2010-06-28 12:17:48 +00:00
VBox layout_vertical = new VBox (false, 12);
HBox layout_horizontal = new HBox (false, 12);
Process process = new Process ();
process.EnableRaisingEvents = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
// TODO: Nice commit summary and "Current Revision"
process.StartInfo.WorkingDirectory = System.IO.Path.GetDirectoryName (file_path);
process.StartInfo.FileName = "git";
process.StartInfo.Arguments = "log --format=\"%ct\t%an\" " + file_name;
process.Start ();
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];
if (i == 0)
revisions_info [i] = "Current Revision" + "\t" + author;
else
revisions_info [i] = UnixTimestampToDateTime (timestamp).ToString ("d MMM\tH:mm") +
"\t" + author;
i++;
}
ViewLeft = new RevisionView (revisions_info);
ViewRight = new RevisionView (revisions_info);
ViewLeft.ComboBox.Active = 1;
ViewRight.ComboBox.Active = 0;
2010-06-30 00:08:02 +00:00
RevisionImage revision_image_left = new RevisionImage (file_path, Revisions [1]);
RevisionImage revision_image_right = new RevisionImage (file_path, Revisions [0]);
ViewLeft.SetImage (revision_image_left);
ViewRight.SetImage (revision_image_right);
ViewLeft.ComboBox.Changed += delegate {
RevisionImage revision_image;
revision_image = new RevisionImage (file_path, Revisions [ViewLeft.ComboBox.Active]);
ViewLeft.SetImage (revision_image);
HookUpViews ();
ViewLeft.ScrolledWindow.Hadjustment = ViewRight.ScrolledWindow.Hadjustment;
ViewLeft.ScrolledWindow.Vadjustment = ViewRight.ScrolledWindow.Vadjustment;
ViewLeft.UpdateControls ();
2010-06-30 00:08:02 +00:00
};
ViewRight.ComboBox.Changed += delegate {
RevisionImage revision_image;
revision_image = new RevisionImage (file_path, Revisions [ViewRight.ComboBox.Active]);
ViewRight.SetImage (revision_image);
HookUpViews ();
ViewRight.ScrolledWindow.Hadjustment = ViewLeft.ScrolledWindow.Hadjustment;
ViewRight.ScrolledWindow.Vadjustment = ViewLeft.ScrolledWindow.Vadjustment;
ViewRight.UpdateControls ();
2010-06-30 00:08:02 +00:00
};
2010-06-28 12:17:48 +00:00
layout_horizontal.PackStart (ViewLeft);
layout_horizontal.PackStart (ViewRight);
2010-06-30 00:08:02 +00:00
HookUpViews ();
2010-06-28 12:17:48 +00:00
HButtonBox dialog_buttons = new HButtonBox ();
dialog_buttons.Layout = ButtonBoxStyle.End;
dialog_buttons.BorderWidth = 0;
Button CloseButton = new Button (Stock.Close);
CloseButton.Clicked += delegate (object o, EventArgs args) {
Environment.Exit (0);
};
dialog_buttons.Add (CloseButton);
layout_vertical.PackStart (layout_horizontal, true, true, 0);
layout_vertical.PackStart (dialog_buttons, false, false, 0);
Add (layout_vertical);
}
2010-06-30 00:08:02 +00:00
// Hooks up two views so they will be kept in sync
private void HookUpViews () {
2010-06-28 12:17:48 +00:00
2010-06-30 00:08:02 +00:00
ViewLeft.ScrolledWindow.Hadjustment.ValueChanged += SyncViewsHorizontally;
ViewLeft.ScrolledWindow.Vadjustment.ValueChanged += SyncViewsVertically;
ViewRight.ScrolledWindow.Hadjustment.ValueChanged += SyncViewsHorizontally;
ViewRight.ScrolledWindow.Vadjustment.ValueChanged += SyncViewsVertically;
2010-06-28 12:17:48 +00:00
}
2010-06-30 00:08:02 +00:00
// Keeps the two image views in sync horizontally
2010-06-28 12:17:48 +00:00
private void SyncViewsHorizontally (object o, EventArgs args) {
Adjustment source_adjustment = (Adjustment) o;
2010-06-30 00:08:02 +00:00
if (source_adjustment == ViewLeft.ScrolledWindow.Hadjustment)
ViewRight.ScrolledWindow.Hadjustment = source_adjustment;
2010-06-28 12:17:48 +00:00
else
2010-06-30 00:08:02 +00:00
ViewLeft.ScrolledWindow.Hadjustment = source_adjustment;
2010-06-28 12:17:48 +00:00
}
2010-06-30 00:08:02 +00:00
// Keeps the two image views in sync vertically
2010-06-28 12:17:48 +00:00
private void SyncViewsVertically (object o, EventArgs args) {
Adjustment source_adjustment = (Adjustment) o;
2010-06-30 00:08:02 +00:00
if (source_adjustment == ViewLeft.ScrolledWindow.Vadjustment)
ViewRight.ScrolledWindow.Vadjustment = source_adjustment;
2010-06-28 12:17:48 +00:00
else
2010-06-30 00:08:02 +00:00
ViewLeft.ScrolledWindow.Vadjustment = source_adjustment;
2010-06-28 12:17:48 +00:00
}
2010-06-30 00:08:02 +00:00
// Gets a list of all earlier revisions of this file
private string [] GetRevisionsForFile (string file_path)
2010-06-28 12:17:48 +00:00
{
2010-06-30 00:08:02 +00:00
string file_name = System.IO.Path.GetFileName (file_path);
2010-06-28 12:17:48 +00:00
Process process = new Process ();
process.EnableRaisingEvents = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
2010-06-30 00:25:02 +00:00
// TODO: Nice commit summary and "Current Revision"
2010-06-30 00:08:02 +00:00
process.StartInfo.WorkingDirectory = System.IO.Path.GetDirectoryName (file_path);
2010-06-28 12:17:48 +00:00
process.StartInfo.FileName = "git";
2010-06-30 00:08:02 +00:00
process.StartInfo.Arguments = "log --format=\"%H\" " + file_name;
2010-06-28 12:17:48 +00:00
process.Start ();
2010-06-30 00:08:02 +00:00
string output = process.StandardOutput.ReadToEnd ();
2010-06-28 12:17:48 +00:00
2010-06-30 00:08:02 +00:00
return Regex.Split (output.Trim (), "\n");
2010-06-28 12:17:48 +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);
}
2010-06-30 00:08:02 +00:00
// Quits the program
private void Quit (object o, EventArgs args) {
2010-06-28 12:17:48 +00:00
2010-06-30 00:08:02 +00:00
Environment.Exit (0);
2010-06-28 12:17:48 +00:00
2010-06-29 10:20:58 +00:00
}
2010-06-28 12:17:48 +00:00
2010-06-29 10:20:58 +00:00
}
2010-06-28 12:17:48 +00:00
2010-06-29 10:20:58 +00:00
// An image grabbed from a stream generated by Git
public class RevisionImage : Image
{
public string Revision;
public string FilePath;
public RevisionImage (string file_path, string revision) : base ()
{
Revision = revision;
FilePath = file_path;
2010-06-28 12:17:48 +00:00
2010-06-29 10:20:58 +00:00
Process process = new Process ();
process.EnableRaisingEvents = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
2010-06-28 12:17:48 +00:00
2010-06-29 10:20:58 +00:00
process.StartInfo.WorkingDirectory = System.IO.Path.GetDirectoryName (FilePath);
process.StartInfo.FileName = "git";
process.StartInfo.Arguments = "show " + revision + ":" + System.IO.Path.GetFileName (FilePath);
process.Start ();
Pixbuf = new Gdk.Pixbuf ((System.IO.Stream) process.StandardOutput.BaseStream);
}
2010-06-28 12:17:48 +00:00
2010-06-29 10:20:58 +00:00
}
// A custom widget containing an image view,
// previous/next buttons and a combobox
2010-06-28 12:17:48 +00:00
public class RevisionView : VBox
{
public ScrolledWindow ScrolledWindow;
public ComboBox ComboBox;
public Button ButtonPrevious;
public Button ButtonNext;
private int ValueCount;
private Image Image;
2010-06-30 00:08:02 +00:00
public RevisionView (string [] revisions) : base (false, 6)
2010-06-28 12:17:48 +00:00
{
Image = new Image ();
ScrolledWindow = new ScrolledWindow ();
ScrolledWindow.AddWithViewport (Image);
PackStart (ScrolledWindow, true, true, 0);
HBox controls = new HBox (false, 6);
2010-06-28 12:17:48 +00:00
controls.BorderWidth = 0;
Image image_previous = new Image ();
image_previous.IconName = "go-previous";
ButtonPrevious = new Button (image_previous);
2010-06-30 00:08:02 +00:00
ButtonPrevious.Clicked += PreviousInComboBox;
2010-06-28 12:17:48 +00:00
ValueCount = 0;
2010-06-30 00:08:02 +00:00
2010-06-28 12:17:48 +00:00
ComboBox = ComboBox.NewText ();
2010-06-30 00:08:02 +00:00
foreach (string revision in revisions) {
ComboBox.AppendText (revision);
}
ComboBox.Active = 0;
ValueCount = revisions.Length;
2010-06-28 12:17:48 +00:00
Image image_next = new Image ();
image_next.IconName = "go-next";
ButtonNext = new Button (image_next);
2010-06-30 00:08:02 +00:00
ButtonNext.Clicked += NextInComboBox;
2010-06-28 12:17:48 +00:00
// controls.PackStart (ButtonPrevious, false, false, 0);
controls.PackStart (ComboBox, false, false, 0);
// controls.PackStart (ButtonNext, false, false, 0);
2010-06-28 12:17:48 +00:00
PackStart (controls, false, false, 0);
2010-06-30 00:08:02 +00:00
UpdateControls ();
2010-06-28 12:17:48 +00:00
}
2010-06-29 10:20:58 +00:00
2010-06-30 00:08:02 +00:00
public void NextInComboBox (object o, EventArgs args) {
/* if (ComboBox.Active > 0)
2010-06-30 00:08:02 +00:00
ComboBox.Active--;
2010-06-28 12:17:48 +00:00
2010-06-30 00:08:02 +00:00
UpdateControls ();
*/
2010-06-30 00:08:02 +00:00
}
public void PreviousInComboBox (object o, EventArgs args) {
/* if (ComboBox.Active + 1 < ValueCount)
2010-06-30 00:08:02 +00:00
ComboBox.Active++;
UpdateControls ();
*/
2010-06-28 12:17:48 +00:00
}
2010-06-29 10:20:58 +00:00
// Changes the image that is viewed
2010-06-28 12:17:48 +00:00
public void SetImage (Image image) {
Image = image;
2010-06-30 00:08:02 +00:00
Remove (ScrolledWindow);
ScrolledWindow = new ScrolledWindow ();
ScrolledWindow.AddWithViewport (Image);
Add (ScrolledWindow);
ReorderChild (ScrolledWindow, 0);
2010-06-28 12:17:48 +00:00
ShowAll ();
}
2010-06-29 10:20:58 +00:00
// Updates the buttons to be disabled or enabled when needed
public void UpdateControls () {
2010-06-28 12:17:48 +00:00
2010-06-30 00:08:02 +00:00
// TODO: Doesn't work yet. Sleepy -.-
/* ButtonPrevious.State = StateType.Normal;
2010-06-30 00:08:02 +00:00
ButtonNext.State = StateType.Normal;
2010-06-28 12:17:48 +00:00
if (ComboBox.Active == 0)
ButtonNext.State = StateType.Insensitive;
2010-06-29 10:20:58 +00:00
2010-06-30 00:08:02 +00:00
if (ComboBox.Active + 1 == ValueCount)
ButtonPrevious.State = StateType.Insensitive;
*/
2010-06-30 00:08:02 +00:00
}
2010-06-28 12:17:48 +00:00
}
}