Skip to content

Commit

Permalink
#8095 Support for environment variables in squiggle settings
Browse files Browse the repository at this point in the history
  • Loading branch information
hasankhan committed Oct 7, 2013
1 parent 352eb43 commit cec71e7
Show file tree
Hide file tree
Showing 17 changed files with 115 additions and 41 deletions.
4 changes: 2 additions & 2 deletions Shared/AssemblyInfo.Version.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: AssemblyVersion("3.2.4.0")]
[assembly: AssemblyFileVersion("3.2.4.0")]
[assembly: AssemblyVersion("3.3.0.0")]
[assembly: AssemblyFileVersion("3.3.0.0")]
9 changes: 5 additions & 4 deletions Squiggle.Client/Chat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ class Chat: IChat
IChatSession session;
ChatBuddies buddies;
IBuddy self;
HistoryManager history;

public Chat(IChatSession session, IBuddy self, IBuddy buddy, BuddyResolver buddyResolver) : this(session, self, Enumerable.Repeat(buddy, 1), buddyResolver) { }

public Chat(IChatSession session, IBuddy self, IEnumerable<IBuddy> buddies, BuddyResolver buddyResolver)
public Chat(IChatSession session, IBuddy self, IEnumerable<IBuddy> buddies, BuddyResolver buddyResolver, HistoryManager history)
{
this.self = self;

Expand All @@ -42,6 +41,8 @@ public Chat(IChatSession session, IBuddy self, IEnumerable<IBuddy> buddies, Budd
session.UserTyping += session_UserTyping;
session.BuzzReceived += session_BuzzReceived;
session.ActivityInviteReceived += session_ActivityInviteReceived;

this.history = history;
}

#region IChat Members
Expand Down Expand Up @@ -273,7 +274,7 @@ void LogSessionStart()
void DoHistoryAction(Action<HistoryManager> action)
{
if (EnableLogging)
ExceptionMonster.EatTheException(() => action(new HistoryManager()), "logging history.");
ExceptionMonster.EatTheException(() => action(history), "logging history.");
}
}
}
11 changes: 6 additions & 5 deletions Squiggle.Client/ChatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class ChatClient: IChatClient
IPresenceService presenceService;
SquiggleEndPoint chatEndPoint;
BuddyList buddies;
HistoryManager history;

public event EventHandler<ChatStartedEventArgs> ChatStarted = delegate { };
public event EventHandler<BuddyOnlineEventArgs> BuddyOnline = delegate { };
Expand Down Expand Up @@ -47,8 +48,9 @@ public bool IsLoggedIn

public bool EnableLogging { get; set; }

public ChatClient(string clientId)
public ChatClient(string clientId, HistoryManager history)
{
this.history = history;
buddies = new BuddyList();
CurrentUser = new SelfBuddy(this, clientId, String.Empty, UserStatus.Offline, new BuddyProperties());
}
Expand All @@ -59,7 +61,7 @@ public IChat StartChat(IBuddy buddy)
throw new InvalidOperationException("Not logged in.");

IChatSession session = chatService.CreateSession(new SquiggleEndPoint(buddy.Id, ((Buddy)buddy).ChatEndPoint));
var chat = new Chat(session, CurrentUser, buddy, id=>buddies[id]);
var chat = new Chat(session, CurrentUser, new[]{ buddy }, id=>buddies[id], history);
return chat;
}

Expand Down Expand Up @@ -121,7 +123,7 @@ void chatService_ChatStarted(object sender, Squiggle.Core.Chat.ChatStartedEventA

if (buddyList.Any())
{
var chat = new Chat(e.Session, CurrentUser, buddyList, id=>buddies[id]);
var chat = new Chat(e.Session, CurrentUser, buddyList, id=>buddies[id], history);
ChatStarted(this, new ChatStartedEventArgs() { Chat = chat, Buddies = buddyList });
}
}
Expand Down Expand Up @@ -196,8 +198,7 @@ void LogStatus(IBuddy buddy)
if (EnableLogging)
ExceptionMonster.EatTheException(() =>
{
var manager = new HistoryManager();
manager.AddStatusUpdate(new Guid(buddy.Id), buddy.DisplayName, (int)buddy.Status);
history.AddStatusUpdate(new Guid(buddy.Id), buddy.DisplayName, (int)buddy.Status);
}, "logging history.");
}

Expand Down
4 changes: 4 additions & 0 deletions Squiggle.History/DAL/Entities/HistoryContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ class HistoryContext : DbContext
public DbSet<Session> Sessions { get; set; }
public DbSet<Event> Events { get; set; }
public DbSet<StatusUpdate> StatusUpdates { get; set; }

public HistoryContext(string nameOrConnectionString): base(nameOrConnectionString)
{
}
}
}
7 changes: 6 additions & 1 deletion Squiggle.History/DAL/HistoryRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ namespace Squiggle.History.DAL
{
class HistoryRepository : IDisposable
{
HistoryContext context = new HistoryContext();
HistoryContext context;

public HistoryRepository(string nameOrConnectionString)
{
this.context = new HistoryContext(nameOrConnectionString);
}

public void AddSessionEvent(Guid sessionId, DateTime stamp, EventType type, Guid sender, string senderName, IEnumerable<Guid> recipients, string data)
{
Expand Down
25 changes: 16 additions & 9 deletions Squiggle.History/HistoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ namespace Squiggle.History
{
public class HistoryManager
{
string connectionString;

public HistoryManager(string connectionString)
{
this.connectionString = connectionString;
}

public void AddSessionEvent(Guid sessionId, EventType type, Guid senderId, string senderName, IEnumerable<Guid> recipients, string data)
{
using (var repository = new HistoryRepository())
using (var repository = new HistoryRepository(connectionString))
{
repository.AddSessionEvent(sessionId, DateTime.UtcNow, type, senderId, senderName, recipients, data);
if (type == EventType.Joined)
Expand All @@ -29,50 +36,50 @@ public void AddSessionEvent(Guid sessionId, EventType type, Guid senderId, strin

public void AddStatusUpdate(Guid contactId, string contactName, int status)
{
using (var repository = new HistoryRepository())
using (var repository = new HistoryRepository(connectionString))
repository.AddStatusUpdate(DateTime.UtcNow, contactId, contactName, status);
}

public IEnumerable<Session> GetSessions(SessionCriteria criteria)
{
using (var repository = new HistoryRepository())
using (var repository = new HistoryRepository(connectionString))
return repository.GetSessions(criteria);
}

public Session GetSession(Guid sessionId)
{
using (var repository = new HistoryRepository())
using (var repository = new HistoryRepository(connectionString))
return repository.GetSession(sessionId);
}


public IEnumerable<StatusUpdate> GetStatusUpdates(StatusCriteria criteria)
{
using (var repository = new HistoryRepository())
using (var repository = new HistoryRepository(connectionString))
return repository.GetStatusUpdates(criteria);
}

public void ClearChatHistory()
{
using (var repository = new HistoryRepository())
using (var repository = new HistoryRepository(connectionString))
repository.ClearChatHistory();
}

public void ClearStatusHistory()
{
using (var repository = new HistoryRepository())
using (var repository = new HistoryRepository(connectionString))
repository.ClearStatusHistory();
}

public void AddSession(Session newSession, IEnumerable<Participant> participants)
{
using (var repository = new HistoryRepository())
using (var repository = new HistoryRepository(connectionString))
repository.AddSession(newSession, participants);
}

public void DeleteSessions(IEnumerable<Guid> sessionIds)
{
using (var repository = new HistoryRepository())
using (var repository = new HistoryRepository(connectionString))
repository.DeleteSessions(sessionIds);
}
}
Expand Down
7 changes: 4 additions & 3 deletions Squiggle.UI/Controls/ChatHistoryViewer.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Squiggle.History.DAL;
using Squiggle.History.DAL.Entities;
using Squiggle.Plugins;
using Squiggle.UI.Factories;
using Squiggle.UI.Helpers;
using Squiggle.UI.Resources;
using Squiggle.UI.Windows;
Expand Down Expand Up @@ -61,7 +62,7 @@ private void results_MouseDoubleClick(object sender, MouseButtonEventArgs e)

void SearchSessions(DateTime? from, DateTime? to, string message)
{
var historyManager = new HistoryManager();
var historyManager = new HistoryManagerFactory().CreateInstance();
var sessions = historyManager.GetSessions(new SessionCriteria()
{
From = from.HasValue ? from.Value.ToUniversalTime() : from,
Expand All @@ -85,7 +86,7 @@ private void Delete_Click(object sender, RoutedEventArgs e)
IEnumerable<Guid> sessionIds = results.SelectedItems.Cast<Result>().Select(r => r.Id).ToList();
AsyncInvoke(() =>
{
var historyManager = new HistoryManager();
var historyManager = new HistoryManagerFactory().CreateInstance();
historyManager.DeleteSessions(sessionIds);
},
lastSearch);
Expand All @@ -98,7 +99,7 @@ private void Clear_Click(object sender, RoutedEventArgs e)
{
AsyncInvoke(() =>
{
var historyManager = new HistoryManager();
var historyManager = new HistoryManagerFactory().CreateInstance();
historyManager.ClearChatHistory();
}, () => results.ItemsSource = null);
}
Expand Down
5 changes: 3 additions & 2 deletions Squiggle.UI/Controls/StatusHistoryViewer.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Squiggle.History;
using Squiggle.History.DAL;
using Squiggle.History.DAL.Entities;
using Squiggle.UI.Factories;
using Squiggle.UI.Resources;
using Squiggle.Utilities;
using Squiggle.Utilities.Threading;
Expand Down Expand Up @@ -44,7 +45,7 @@ void Search_Click(object sender, RoutedEventArgs e)

void SearchUpdates(DateTime? from, DateTime? to)
{
var historyManager = new HistoryManager();
var historyManager = new HistoryManagerFactory().CreateInstance();
var updates = historyManager.GetStatusUpdates(new StatusCriteria()
{
From = from.HasValue ? from.Value.ToUniversalTime() : from,
Expand All @@ -65,7 +66,7 @@ private void Clear_Click(object sender, RoutedEventArgs e)
{
AsyncInvoke(() =>
{
var historyManager = new HistoryManager();
var historyManager = new HistoryManagerFactory().CreateInstance();
historyManager.ClearStatusHistory();
}, () => results.ItemsSource = null);
}
Expand Down
22 changes: 22 additions & 0 deletions Squiggle.UI/Factories/HistoryManagerFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using Squiggle.History;

namespace Squiggle.UI.Factories
{
class HistoryManagerFactory: IInstanceFactory<HistoryManager>
{
public HistoryManager CreateInstance()
{
ConnectionStringSettings setting = ConfigurationManager.ConnectionStrings["HistoryContext"];
if (setting == null)
return null;

string connectionString = Environment.ExpandEnvironmentVariables(setting.ConnectionString);
return new HistoryManager(connectionString);
}
}
}
19 changes: 15 additions & 4 deletions Squiggle.UI/Factories/LoginOptionsFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public LoginOptions CreateInstance()

string clientID = settings.ConnectionSettings.ClientID;

string displayName = Environment.ExpandEnvironmentVariables(userInfo.DisplayName.NullIfEmpty() ??
signInOptions.DisplayName.NullIfEmpty() ??
settings.PersonalSettings.DisplayName);
var options = new LoginOptions()
{
ChatEndPoint = chatEndPoint,
Expand All @@ -56,19 +59,27 @@ public LoginOptions CreateInstance()
PresenceServiceEndPoint = presenceServiceEndPoint,
KeepAliveTime = keepAliveTimeout,
UserProperties = CreateProperties(),
DisplayName = userInfo.DisplayName.NullIfEmpty() ?? signInOptions.DisplayName.NullIfEmpty() ?? settings.PersonalSettings.DisplayName
DisplayName = displayName
};

return options;
}

IBuddyProperties CreateProperties()
{
string groupName = Environment.ExpandEnvironmentVariables(userInfo.GroupName.NullIfEmpty() ??
signInOptions.GroupName.NullIfEmpty() ??
settings.PersonalSettings.GroupName);

string email = Environment.ExpandEnvironmentVariables(userInfo.Email.NullIfEmpty() ?? settings.PersonalSettings.EmailAddress);

string displayMessage = Environment.ExpandEnvironmentVariables(settings.PersonalSettings.DisplayMessage);

var properties = new BuddyProperties();
properties.GroupName = userInfo.GroupName.NullIfEmpty() ?? signInOptions.GroupName.NullIfEmpty() ?? settings.PersonalSettings.GroupName;
properties.EmailAddress = userInfo.Email.NullIfEmpty() ?? settings.PersonalSettings.EmailAddress;
properties.GroupName = groupName;
properties.EmailAddress = email;
properties.DisplayImage = userInfo.Image ?? settings.PersonalSettings.DisplayImage;
properties.DisplayMessage = settings.PersonalSettings.DisplayMessage;
properties.DisplayMessage = displayMessage;
properties.MachineName = Environment.MachineName;

return properties;
Expand Down
10 changes: 8 additions & 2 deletions Squiggle.UI/Factories/SquiggleContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using Squiggle.Client;
using Squiggle.History;
using Squiggle.UI.Components;
using Squiggle.UI.Windows;

Expand All @@ -11,12 +12,17 @@ namespace Squiggle.UI.Factories
class SquiggleContextFactory: IInstanceFactory<SquiggleContext>
{
IInstanceFactory<PluginLoader> pluginLoaderFactory;
HistoryManager history;
MainWindow window;
string clientId;

public SquiggleContextFactory(IInstanceFactory<PluginLoader> pluginLoaderFactory, MainWindow window, string clientId)
public SquiggleContextFactory(IInstanceFactory<PluginLoader> pluginLoaderFactory,
HistoryManager history,
MainWindow window,
string clientId)
{
this.pluginLoaderFactory = pluginLoaderFactory;
this.history = history;
this.window = window;
this.clientId = clientId;
}
Expand All @@ -29,7 +35,7 @@ public SquiggleContext CreateInstance()
SquiggleContext context = new SquiggleContext();
context.MainWindow = window;
context.PluginLoader = pluginLoader;
context.ChatClient = new ChatClient(clientId);
context.ChatClient = new ChatClient(clientId, history);
SquiggleContext.Current = context;
}
return SquiggleContext.Current;
Expand Down
15 changes: 10 additions & 5 deletions Squiggle.UI/Helpers/SquiggleUtility.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Windows;
using Squiggle.Client;
using Squiggle.Core.Presence;
using Squiggle.UI.Components;
using Squiggle.UI.Controls;
using Squiggle.UI.Settings;
using Squiggle.UI.ViewModel;
using Squiggle.UI.Windows;
using Squiggle.Utilities;
using Squiggle.Utilities.Application;
using Squiggle.UI.Windows;
using Squiggle.UI.Components;

namespace Squiggle.UI.Helpers
{
Expand All @@ -34,9 +32,16 @@ public static IEnumerable<UserStatus> GetChangableStatuses()
return statuses;
}

public static void OpenDownloadsFolder()
public static string GetDownloadsFolderPath()
{
string downloadsFolder = SettingsProvider.Current.Settings.GeneralSettings.DownloadsFolder;
downloadsFolder = Environment.ExpandEnvironmentVariables(downloadsFolder);
return downloadsFolder;
}

public static void OpenDownloadsFolder()
{
string downloadsFolder = GetDownloadsFolderPath();
if (Shell.CreateDirectoryIfNotExists(downloadsFolder))
ExceptionMonster.EatTheException(() => Process.Start(downloadsFolder), "opening downloads folder");
}
Expand Down
Loading

0 comments on commit cec71e7

Please sign in to comment.