2010-05-06 00:17:00 +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/>.
|
|
|
|
|
2010-05-10 13:42:46 +00:00
|
|
|
using Gtk;
|
2010-05-06 00:17:00 +00:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Net;
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
using System.Text;
|
2010-05-13 20:29:21 +00:00
|
|
|
using System.Text.RegularExpressions;
|
2010-05-06 00:17:00 +00:00
|
|
|
|
|
|
|
namespace SparkleShare {
|
|
|
|
|
|
|
|
public static class SparkleHelpers {
|
|
|
|
|
2010-05-10 22:20:17 +00:00
|
|
|
public static Gdk.Pixbuf GetAvatar (string Email, int Size) {
|
2010-05-06 00:17:00 +00:00
|
|
|
|
2010-05-08 14:31:44 +00:00
|
|
|
string AvatarPath = Path.Combine (SparklePaths.SparkleAvatarPath,
|
|
|
|
Size + "x" + Size);
|
2010-05-06 00:17:00 +00:00
|
|
|
|
|
|
|
if (!Directory.Exists (AvatarPath)) {
|
|
|
|
Directory.CreateDirectory (AvatarPath);
|
|
|
|
Console.WriteLine ("[Config] Created '" + AvatarPath + "'");
|
|
|
|
}
|
|
|
|
|
2010-05-08 14:31:44 +00:00
|
|
|
string AvatarFilePath = AvatarPath + Email;
|
2010-05-06 00:17:00 +00:00
|
|
|
|
2010-05-08 14:31:44 +00:00
|
|
|
if (File.Exists (AvatarFilePath))
|
2010-05-10 22:20:17 +00:00
|
|
|
return new Gdk.Pixbuf (AvatarFilePath);
|
2010-05-06 00:17:00 +00:00
|
|
|
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-05-08 14:31:44 +00:00
|
|
|
string TmpFile = SparklePaths.SparkleTmpPath + Email + Size;
|
2010-05-06 00:17:00 +00:00
|
|
|
|
|
|
|
if (!File.Exists (TmpFile)) {
|
|
|
|
|
|
|
|
WebClient.DownloadFileAsync (GravatarUri, TmpFile);
|
|
|
|
WebClient.DownloadFileCompleted += delegate {
|
|
|
|
File.Delete (AvatarPath + Email);
|
|
|
|
FileInfo TmpFileInfo = new FileInfo (TmpFile);
|
|
|
|
if (TmpFileInfo.Length > 255)
|
|
|
|
File.Move (TmpFile, AvatarPath + Email);
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-05-08 15:41:54 +00:00
|
|
|
if (File.Exists (AvatarPath + Email))
|
2010-05-10 22:20:17 +00:00
|
|
|
return new Gdk.Pixbuf (AvatarPath + Email);
|
2010-05-06 00:17:00 +00:00
|
|
|
else
|
2010-05-10 22:20:17 +00:00
|
|
|
return GetIcon ("avatar-default", Size);
|
2010-05-08 14:38:21 +00:00
|
|
|
|
2010-05-06 00:17:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-05-08 14:38:21 +00:00
|
|
|
// Creates an MD5 hash
|
2010-05-06 00:17:00 +00:00
|
|
|
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-05-08 14:31:44 +00:00
|
|
|
|
2010-05-08 14:38:21 +00:00
|
|
|
// Makes it possible to combine more than
|
|
|
|
// two paths at once.
|
2010-05-08 14:31:44 +00:00
|
|
|
public static string CombineMore (params string [] Parts) {
|
|
|
|
string NewPath = "";
|
|
|
|
foreach (string Part in Parts)
|
2010-05-10 13:42:46 +00:00
|
|
|
NewPath = Path.Combine (NewPath, Part);
|
2010-05-08 14:31:44 +00:00
|
|
|
return NewPath;
|
|
|
|
}
|
2010-05-06 00:17:00 +00:00
|
|
|
|
2010-05-10 13:42:46 +00:00
|
|
|
public static IconTheme SparkleTheme = new IconTheme ();
|
|
|
|
|
|
|
|
// Looks up an icon from the system's theme
|
|
|
|
public static Gdk.Pixbuf GetIcon (string Name, int Size) {
|
2010-05-10 22:20:17 +00:00
|
|
|
SparkleTheme.AppendSearchPath (SparklePaths.SparkleInstallPath);
|
2010-05-10 13:42:46 +00:00
|
|
|
return SparkleTheme.LoadIcon (Name, Size,
|
|
|
|
IconLookupFlags.GenericFallback);
|
|
|
|
}
|
|
|
|
|
2010-05-12 09:47:00 +00:00
|
|
|
public static bool IsGitUrl (string Url) {
|
2010-05-13 22:44:16 +00:00
|
|
|
return Regex.Match (Url, @"[a-z]+://(.)+").Success;
|
2010-05-12 09:47:00 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 00:17:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|