Roaming Options for User Settings: Listing 3

Retrieving cloud settings.

public static void RetrieveCloudSettings()
{
    LiveClient.GetAsync("/me/skydrive/my_documents/files/",false);
}

private static void FindCloudSettingsFile(IDictionary<string, object> result)
{
    var data = (List<object>)result["data"];

    foreach (IDictionary<string, object> album in data)
    {
        var filename = album.SafeValue<string>("name");
        if (filename == CloudSettingsFile)
        {
            var source = album.SafeValue<string>("source");
            LiveClient.DownloadAsync(source);
            return;
        }
    }
}

private static void FileDownloadCompleted(object sender, LiveDownloadCompletedEventArgs e)
{
    var tmpFile = Guid.NewGuid().ToString();
    try
    {
        using (var file = IsolatedStorageFile.GetUserStoreForApplication().OpenFile(tmpFile,
                                                                                    FileMode.Create,
                                                                                    FileAccess.Write))
        {
            e.Result.CopyTo(file);
        }

        ReloadSettings(tmpFile);

        if (File.Exists(tmpFile))
        {
            File.Delete(tmpFile);
        }
    }
    catch(Exception ex)
    {
                
    }
}

private static void ReloadSettings(string temporarySettingsFile)
{
    var settings = new Dictionary<string, object>();
    using (var file = IsolatedStorageFile.GetUserStoreForApplication().OpenFile(temporarySettingsFile, FileMode.Open,
      FileAccess.Read))
    {
        using (var streamReader = new StreamReader(file))
        {
            if (file.Length > 0)
            {
                try
                {
                    var list = new List<Type>();
                    var text = streamReader.ReadLine()+"";
                    var separator = new char[1];
                    var array = text.Split(separator);
                    for (int i = 0; i < array.Length; i++)
                    {
                        var typeName = array[i];
                        Type type = Type.GetType(typeName, false);
                        if (type != null)
                        {
                            list.Add(type);
                        }
                    }
                    file.Position = text.Length + Environment.NewLine.Length;
                    var dataContractSerializer = new DataContractSerializer(typeof(Dictionary<string, object>), list);
                    settings = (Dictionary<string, object>)dataContractSerializer.ReadObject(file);
                }
                catch(Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                    return;
                }
            }
        }
    }
    IsolatedStorageSettings.ApplicationSettings.Clear();
    foreach (var setting in settings)
    {
        IsolatedStorageSettings.ApplicationSettings[setting.Key] = setting.Value;
    }
    IsolatedStorageSettings.ApplicationSettings.Save();

    SettingsRetrievedFromCloud(LiveClient, EventArgs.Empty);
}

About the Author

Nick Randolph runs Built to Roam, a consulting company that specializes in training, mentoring and assisting other companies build mobile applications. With a heritage in rich client applications for both the desktop and a variety of mobile platforms, Nick currently presents, writes and educates on the Windows Phone platform.

comments powered by Disqus

Featured

Subscribe on YouTube