Continue work on FriendFace

This commit is contained in:
Hylke Bons 2010-07-18 19:38:34 +01:00
parent d1523a7fd3
commit 0a629c2a65
6 changed files with 285 additions and 216 deletions

View file

@ -15,88 +15,137 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using Gtk;
using Mono.Unix;
using System;
using System.IO;
namespace FriendFace
{
namespace FriendFace {
public class FaceCollection : IconTheme
{
public class FaceCollection : IconTheme {
private string Path;
public bool UseTwitter;
public bool UseFlickr;
public bool UseGravatar;
public bool UseIdentica;
public bool UseSystem;
public bool UseFlickr;
public bool UseAllServices;
public bool UseTwitter;
private string IconThemePath;
private string Path;
public FaceCollection ()
{
Path = "";
UseTwitter = false;
UseGravatar = false;
UseIdentica = false;
UseSystem = false;
UseFlickr = false;
UseAllServices = false;
UnixUserInfo unix_user_info = new UnixUserInfo (UnixEnvironment.UserName);
string default_theme_path = CombineMore (unix_user_info.HomeDirectory, ".icons");
new FaceCollection (default_theme_path);
}
public FaceCollection (string path)
{
UnixUserInfo unix_user_info = new UnixUserInfo (UnixEnvironment.UserName);
string home_path = unix_user_info.HomeDirectory;
string IconThemePath = CombineMore (home_path, ".icons");
SetThemePath (IconThemePath);
if (!Directory.Exists (theme_path))
Directory.CreateDirectory (theme_path);
Directory.CreateDirectory (CombineMore (ThemePath));
CustomTheme = "FriendFace";
SetThemePath (path);
}
public void SetThemePath (string path)
{
string IconThemePath = path;
Path = path;
if (!Directory.Exists (Path))
Directory.CreateDirectory (Path);
AppendSearchPath (path);
Refresh ();
}
public string GetThemePath ()
{
return Path;
}
public Gdk.Pixbuf GetFace (string identifier)
public Gdk.Pixbuf GetFace (string identifier, int size)
{
return null;
return LoadIcon ("avatar-default-" + identifier, size, IconLookupFlags.GenericFallback);
}
public void Refresh ()
{foreach (string i in SearchPath) {Console.WriteLine (i);}
IconProvider provider = new IconProvider ("");
string folder = provider.GetTargetFolderPath ();
string [] files = Directory.GetFiles (folder);
int [] sizes = {16, 24, 32, 48};
foreach (string file_path in files) {
Gdk.Pixbuf pixbuf = new Gdk.Pixbuf (file_path);
FileInfo file_info = new FileInfo (file_path);
// Delete the icon if it turns out to be empty
if (file_info.Length == 0) {
File.Delete (file_path);
Console.WriteLine ("Deleted: " + file_path);
}
for (int i = 0; i < 4; i++) {
int size = sizes [i];
Gdk.Pixbuf pixbuf_copy = pixbuf.Copy ();
if (pixbuf.Width != size || pixbuf.Height != size)
pixbuf_copy = pixbuf.ScaleSimple (size, size, Gdk.InterpType.Hyper);
string size_folder_path = CombineMore (Path, size + "x" + size, "status");
string size_file_path = CombineMore (size_folder_path, System.IO.Path.GetFileName (file_path));
Directory.CreateDirectory (size_folder_path);
if (File.Exists (size_file_path))
File.Delete (size_file_path);
pixbuf_copy.Save (size_file_path, "png");
}
File.Delete (file_path);
}
RescanIfNeeded ();
}
public bool AddFace (string identifier)
{
// avatar-twitter-hbons
// 16, 24, 32, 48
Gdk.Pixbuf gravatar_icon;
if (UseGravatar)
gravatar_icon = new GravatarIcon (identifier);
if (UseGravatar) {
GravatarIconProvider provider = new GravatarIconProvider (identifier);
provider.RetrieveIcon ();
}
return true;
}
public bool Refresh ()
// Makes it possible to combine more than
// two paths at once
private string CombineMore (params string [] parts)
{
return true;
string new_path = "";
foreach (string part in parts)
new_path = System.IO.Path.Combine (new_path, part);
return new_path;
}
}

View file

@ -1,79 +0,0 @@
// FriendFace creates an icon theme of buddy icons from the web
// Copyright (C) 2010 Hylke Bons
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.using Gtk;
using Gtk;
using System;
namespace FriendFace {
public class Gravatar : Gdk.Pixbuf {
public Gravatar (string identifier)
{
UnixUserInfo unix_user_info = new UnixUserInfo (UnixEnvironment.UserName);
string home_path = unix_user_info.HomeDirectory;
string theme_path = CombineMore (home_path, ".icons", "friendface", "48x48", "status");
string file_name = "avatar-gravatar-" + identifier;
string file_path = CombineMore (theme_path, file_name);
if (!Directory.Exists (theme_path))
Directory.CreateDirectory (theme_path);
WebClient WebClient = new WebClient ();
Uri icon_uri = new Uri ("http://www.gravatar.com/avatar/" + MD5 (identifier) + ".jpg?s=48&d=404");
if (File.Exists (file_path))
File.Delete (file_path);
WebClient.DownloadFileAsync (icon_uri, file_path);
WebClient.DownloadFileCompleted += delegate {
FileInfo file_info = new FileInfo (file_path);
if (file_info.Length < 256)
File.Delete (file_path);
};
}
}
// Creates an MD5 hash of input
public static string MD5 (string s)
{
MD5 md5 = new MD5CryptoServiceProvider ();
Byte[] bytes = ASCIIEncoding.Default.GetBytes (s);
Byte[] encoded_bytes = md5.ComputeHash (bytes);
return BitConverter.ToString (encoded_bytes).ToLower ().Replace ("-", "");
}
// Makes it possible to combine more than
// two paths at once
public static string CombineMore (params string [] parts)
{
string new_path = "";
foreach (string part in parts)
new_path = Path.Combine (new_path, part);
return new_path;
}
}

View file

@ -0,0 +1,66 @@
// FriendFace creates an icon theme of buddy icons from the web
// Copyright (C) 2010 Hylke Bons
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.using Gtk;
using Gtk;
using Mono.Unix;
using System;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
namespace FriendFace {
public class GravatarIconProvider : IconProvider {
public GravatarIconProvider (string email) : base (email)
{
ServiceName = "gravatar";
}
new public void RetrieveIcon ()
{
string target_file_path = GetTargetFilePath ();
if (File.Exists (target_file_path))
return;
WebClient web_client = new WebClient ();
Uri icon_uri = new Uri ("http://www.gravatar.com/avatar/" + MD5 (Identifier) + ".jpg?s=48&d=404");
web_client.DownloadFileAsync (icon_uri, target_file_path);
web_client.DownloadFileCompleted += delegate {
base.RetrieveIcon ();
};
}
// Creates an MD5 hash of input
public static string MD5 (string s)
{
MD5 md5 = new MD5CryptoServiceProvider ();
Byte[] bytes = ASCIIEncoding.Default.GetBytes (s);
Byte[] encoded_bytes = md5.ComputeHash (bytes);
return BitConverter.ToString (encoded_bytes).ToLower ().Replace ("-", "");
}
}
}

110
FriendFace/IconProvider.cs Normal file
View file

@ -0,0 +1,110 @@
// IconProvider is a base class that can be extended to pull buddy
// icons from sources so that they can be used by FaceCollection.
//
// Copyright (C) 2010 Hylke Bons
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.using Gtk;
using Gtk;
using Mono.Unix;
using System;
using System.IO;
namespace FriendFace {
public class IconProvider {
public string Identifier;
public string ServiceName;
private string TargetFolderPath;
private string TargetFilePath;
public IconProvider (string identifier)
{
Identifier = identifier;
string XDG_CACHE_HOME = Environment.GetEnvironmentVariable ("XDG_CACHE_HOME");
if (XDG_CACHE_HOME != null)
SetTargetFolderPath (CombineMore (XDG_CACHE_HOME, "friendface"));
else
SetTargetFolderPath (CombineMore (System.IO.Path.DirectorySeparatorChar.ToString (), "tmp", "friendface"));
string file_name = "avatar-default-" + Identifier;
TargetFilePath = CombineMore (TargetFolderPath, file_name);
}
public void RetrieveIcon ()
{
if (File.Exists (TargetFilePath)) {
FileInfo file_info = new FileInfo (TargetFilePath);
// Delete the icon if it turns out to be empty
if (file_info.Length == 0) {
File.Delete (TargetFilePath);
Console.WriteLine ("Deleted: " + TargetFilePath);
}
}
}
public void SetTargetFolderPath (string path)
{
TargetFolderPath = path;
if (!Directory.Exists (TargetFolderPath))
Directory.CreateDirectory (TargetFolderPath);
}
public string GetTargetFolderPath ()
{
return TargetFolderPath;
}
public void SetTargetFilePath (string path)
{
TargetFilePath = path;
}
public string GetTargetFilePath ()
{
return TargetFilePath;
}
// Makes it possible to combine more than two paths at once
public string CombineMore (params string [] parts)
{
string new_path = "";
foreach (string part in parts)
new_path = Path.Combine (new_path, part);
return new_path;
}
}
}

View file

@ -5,6 +5,9 @@ LINK = $(REF_SPARKLEDIFF)
SOURCES = \
$(top_srcdir)/SparkleShare/Defines.cs \
$(top_srcdir)/FriendFace/FriendFace.cs \
$(top_srcdir)/FriendFace/IconProvider.cs \
$(top_srcdir)/FriendFace/GravatarIconProvider.cs \
SparkleDiff.cs \
SparkleDiffWindow.cs \
RevisionView.cs \

View file

@ -14,17 +14,13 @@
// 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 FriendFace;
using Gtk;
using Mono.Unix;
using System;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
namespace SparkleShare {
// The main window for SparkleDiff
@ -52,6 +48,9 @@ namespace SparkleShare {
BorderWidth = 12;
IconName = "image-x-generic";
FaceCollection face_collection = new FaceCollection ();
face_collection.UseGravatar = true;
DeleteEvent += Quit;
Title = file_name;
@ -94,8 +93,10 @@ namespace SparkleShare {
UnixTimestampToDateTime (timestamp).ToString (_("ddd MMM d, yyyy")),
UnixTimestampToDateTime (timestamp).ToString (_("H:mm")));
ViewLeft.AddRow (GetAvatar (email, 32), author, date);
ViewRight.AddRow (GetAvatar (email, 32), author, date);
face_collection.AddFace (email);
ViewLeft.AddRow (face_collection.GetFace (email, 32), author, date);
ViewRight.AddRow (face_collection.GetFace (email, 32), author, date);
i++;
@ -243,15 +244,6 @@ namespace SparkleShare {
}
public string CombineMore (params string [] Parts)
{
string NewPath = " ";
foreach (string Part in Parts)
NewPath = System.IO.Path.Combine (NewPath, Part);
return NewPath;
}
// Converts a UNIX timestamp to a more usable time object
public DateTime UnixTimestampToDateTime (int timestamp)
{
@ -260,78 +252,6 @@ namespace SparkleShare {
}
// 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);
}
// 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 ("-", "");
}
// TODO: Turn this into an avatar fetching library
// 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;
string SparkleLocalIconPath = CombineMore (HomePath, ".icons", "sparkleshare");
string AvatarPath = CombineMore (SparkleLocalIconPath, Size + "x" + Size, "status");
if (!Directory.Exists (AvatarPath)) {
Directory.CreateDirectory (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");
string TmpFile = CombineMore (HomePath, "SparkleShare", ".tmp", Email + Size);
if (!File.Exists (TmpFile)) {
WebClient.DownloadFileAsync (GravatarUri, TmpFile);
WebClient.DownloadFileCompleted += delegate {
File.Delete (AvatarFilePath);
FileInfo TmpFileInfo = new FileInfo (TmpFile);
if (TmpFileInfo.Length > 255)
File.Move (TmpFile, AvatarFilePath);
};
}
// 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);
}
}
// Quits the program
private void Quit (object o, EventArgs args)
{