[osx] Fix crashes in statusitem, caused by freed memory

This commit is contained in:
Hylke Bons 2010-11-17 23:40:52 +00:00
parent 8f6a6f0710
commit 0b51bab957
3 changed files with 159 additions and 110 deletions

View file

@ -10,6 +10,12 @@ namespace SparkleShare
public partial class AppDelegate : NSApplicationDelegate public partial class AppDelegate : NSApplicationDelegate
{ {
MainWindowController mainWindowController; MainWindowController mainWindowController;
NSStatusItem statusItem;
NSMenu menu;
NSMenuItem item;
public AppDelegate () public AppDelegate ()
{ {
@ -23,7 +29,7 @@ namespace SparkleShare
// SparkleStatusIcon = new SparkleStatusIcon (); // SparkleStatusIcon = new SparkleStatusIcon ();
SparkleRepo repo = new SparkleRepo ("/Users/hbons/SparkleShare/SparkleShare-Test"); SparkleRepo repo = new SparkleRepo ("/Users/hbons/SparkleShare/SparkleShare-Test");
var statusItem = NSStatusBar.SystemStatusBar.CreateStatusItem (32); statusItem = NSStatusBar.SystemStatusBar.CreateStatusItem (32);
statusItem.Enabled = true; statusItem.Enabled = true;
@ -32,11 +38,11 @@ namespace SparkleShare
statusItem.Image.Size = new SizeF (16, 16); statusItem.Image.Size = new SizeF (16, 16);
statusItem.AlternateImage.Size = new SizeF (16, 16); statusItem.AlternateImage.Size = new SizeF (16, 16);
NSMenu menu = new NSMenu() {}; menu = new NSMenu() {};
menu.AddItem (new NSMenuItem () { Title="Up to date (102 MB)", Enabled = true }); // menu.AddItem (new NSMenuItem () { Title="Up to date (102 MB)", Enabled = true });
menu.AddItem (NSMenuItem.SeparatorItem); // menu.AddItem (NSMenuItem.SeparatorItem);
var item = new NSMenuItem () { item = new NSMenuItem () {
Title="SparkleShare", Enabled = true, Title="SparkleShare", Enabled = true,
Action = new Selector ("ddd") Action = new Selector ("ddd")
}; };
@ -50,7 +56,7 @@ namespace SparkleShare
menu.AddItem (item); menu.AddItem (item);
var tmp = new NSMenuItem () { /* var tmp = new NSMenuItem () {
Title="gnome-design", Enabled = true, Title="gnome-design", Enabled = true,
Action = new Selector ("ddd") Action = new Selector ("ddd")
}; };
@ -116,7 +122,7 @@ namespace SparkleShare
menu.AddItem (quit_menu_item); menu.AddItem (quit_menu_item);
*/
statusItem.Menu = menu; statusItem.Menu = menu;
statusItem.HighlightMode = true; statusItem.HighlightMode = true;

View file

@ -23,7 +23,7 @@
<ConsolePause>false</ConsolePause> <ConsolePause>false</ConsolePause>
<EnvironmentVariables> <EnvironmentVariables>
<EnvironmentVariables> <EnvironmentVariables>
<Variable name="PATH" value="" /> <Variable name="PATH" value="/opt/local/bin" />
</EnvironmentVariables> </EnvironmentVariables>
</EnvironmentVariables> </EnvironmentVariables>
</PropertyGroup> </PropertyGroup>

View file

@ -1,128 +1,171 @@
using System; // SparkleShare, an instant update workflow to Git.
using System.Drawing; // Copyright (C) 2010 Hylke Bons <hylkebons@gmail.com>
using MonoMac.Foundation; //
using MonoMac.AppKit; // This program is free software: you can redistribute it and/or modify
using MonoMac.ObjCRuntime; // 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 Mono.Unix.Native;
using SparkleLib; using SparkleLib;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
namespace SparkleShare namespace SparkleShare {
{
[MonoMac.Foundation.Register("AppDelegate")] public class SparkleUI {
public class SparkleUI : NSApplicationDelegate
{ public static SparkleStatusIcon StatusIcon;
public void Run () { public static List <SparkleLog> OpenLogs;
NSApplication.Main (new string [1] {""});
// Short alias for the translations
public static string _(string s)
{
return Catalog.GetString (s);
} }
public SparkleUI () {NSApplication.Init ();
Console.WriteLine("HI!2");
// SparkleFetcher fetch = new SparkleFetcher ("", "");
//SparkleRepo repo = new SparkleRepo ("/Users/hbons/Code/SparkleShare");
// SparkleStatusIcon = new SparkleStatusIcon ();
var statusItem = NSStatusBar.SystemStatusBar.CreateStatusItem (32); public SparkleUI ()
{
statusItem.Enabled = true; // Initialize the application
Application.Init ();
statusItem.Image = NSImage.ImageNamed ("sparkleshare-idle.png"); // Create the statusicon
statusItem.AlternateImage = NSImage.ImageNamed ("sparkleshare-idle-focus.png"); StatusIcon = new SparkleStatusIcon ();
statusItem.Image.Size = new SizeF (16, 16);
statusItem.AlternateImage.Size = new SizeF (16, 16);
NSMenu menu = new NSMenu() {}; // Keep track of which event logs are open
menu.AddItem (new NSMenuItem () { Title="Up to date (102 MB)", Enabled = true }); SparkleUI.OpenLogs = new List <SparkleLog> ();
menu.AddItem (NSMenuItem.SeparatorItem);
var item = new NSMenuItem () { SparkleShare.Controller.OnFirstRun += delegate {
Title="SparkleShare", Enabled = true, Application.Invoke (delegate {
Action = new Selector ("ddd")
SparkleIntro intro = new SparkleIntro ();
intro.ShowAll ();
});
}; };
item.Activated += delegate { SparkleShare.Controller.OnInvitation += delegate (string invitation_file_path) {
Console.WriteLine ("DDDD"); Application.Invoke (delegate {
};
item.Image = NSImage.ImageNamed ("NSFolder"); SparkleInvitation invitation = new SparkleInvitation (invitation_file_path);
item.Image.Size = new SizeF (16, 16); invitation.Present ();
menu.AddItem (item); });
var tmp = new NSMenuItem () {
Title="gnome-design", Enabled = true,
Action = new Selector ("ddd")
}; };
tmp.Activated += delegate { // Show a bubble when there are new changes
Console.WriteLine ("DDDD"); SparkleShare.Controller.NotificationRaised += delegate (SparkleCommit commit, string repository_path) {
};
tmp.Image = NSImage.ImageNamed ("NSFolder"); string file_name = "";
tmp.Image.Size = new SizeF (16, 16); string message = null;
menu.AddItem (tmp); if (commit.Added.Count > 0) {
menu.AddItem (NSMenuItem.SeparatorItem);
Console.WriteLine (item.Action.Name); foreach (string added in commit.Added) {
file_name = added;
break;
}
message = String.Format (_("added {0}"), file_name);
}
if (commit.Edited.Count > 0) {
foreach (string modified in commit.Edited) {
file_name = modified;
break;
}
message = String.Format (_("edited {0}"), file_name);
}
if (commit.Deleted.Count > 0) {
foreach (string removed in commit.Deleted) {
file_name = removed;
break;
}
message = String.Format (_("deleted {0}"), file_name);
}
int changes_count = (commit.Added.Count +
commit.Edited.Count +
commit.Deleted.Count);
if (changes_count > 1)
message += " + " + (changes_count - 1);
Application.Invoke (delegate {
SparkleBubble bubble = new SparkleBubble (commit.UserName, message);
string avatar_file_path = SparkleUIHelpers.GetAvatar (commit.UserEmail, 32);
if (avatar_file_path != null)
bubble.Icon = new Gdk.Pixbuf (avatar_file_path);
else
bubble.Icon = SparkleUIHelpers.GetIcon ("avatar-default", 32);
// bubble.AddAction ("", "Show Events", delegate {
// SparkleLog log = new SparkleLog (repository_path);
// log.ShowAll ();
// });
// bubble.Show ();
});
NSMenuItem sync_menu_item = new NSMenuItem () {
Title = "Sync Remote Folder..."
}; };
sync_menu_item.Activated += delegate { // Show a bubble when there was a conflict
Console.WriteLine ("DDDD"); SparkleShare.Controller.ConflictNotificationRaised += delegate {
}; Application.Invoke (delegate {
menu.AddItem (sync_menu_item); string title = _("Ouch! Mid-air collision!");
menu.AddItem (NSMenuItem.SeparatorItem); string subtext = _("Don't worry, SparkleShare made a copy of each conflicting file.");
NSMenuItem notifications_menu_item = new NSMenuItem () { SparkleBubble bubble = new SparkleBubble(title, subtext);
Title = "Show Notifications", // bubble.Show ();
State = NSCellStateValue.On
});
}; };
notifications_menu_item.Activated += delegate {
statusItem.Image = NSImage.ImageNamed ("NSComputer");
if (notifications_menu_item.State == NSCellStateValue.On)
notifications_menu_item.State = NSCellStateValue.Off;
else
notifications_menu_item.State = NSCellStateValue.On;
};
menu.AddItem (notifications_menu_item);
menu.AddItem (NSMenuItem.SeparatorItem);
NSMenuItem about_menu_item = new NSMenuItem () {
Title = "About"
};
about_menu_item.Activated += delegate {
Console.WriteLine ("DDDD");
statusItem.Title = "bla";
};
menu.AddItem (about_menu_item);
menu.AddItem (NSMenuItem.SeparatorItem);
NSMenuItem quit_menu_item = new NSMenuItem () {
Title = "Quit"
};
quit_menu_item.Activated += delegate {
Console.WriteLine ("DDDD");
Environment.Exit (0);
};
menu.AddItem (quit_menu_item);
statusItem.Menu = menu;
statusItem.HighlightMode = true;
} }
// Runs the application
public void Run ()
{
Application.Run ();
}
} }
} }