void signIn()
        {
            new MastodonAPIWrapper();
            OAuthUtils oAuthUtils = MastodonAPIWrapper.sharedApiWrapper.sharedOAuthUtils;

            SignInWindow signInWindow = new SignInWindow();

            signInWindow.callbackDelegate = this;
            signInWindow.ShowDialog();

            if (this.authenticated == true)
            {
                BackgroundWorker worker2 = new BackgroundWorker();
                worker2.DoWork += (sender2, e2) =>
                {
                    if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Capella"))
                    {
                        Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Capella");
                    }
                    JObject json = new JObject();

                    JArray accounts = new JArray();

                    if (MastodonAPIWrapper.sharedApiWrapper.accounts != null)
                    {
                        foreach (Account mastodonAccount in MastodonAPIWrapper.sharedApiWrapper.accounts)
                        {
                            JObject rawAccount = new JObject();
                            if (mastodonAccount.accessToken.Equals(accountToken))
                            {
                                return;
                            }
                            rawAccount.Add("token", mastodonAccount.accessToken);
                            rawAccount.Add("endpoint", mastodonAccount.endpoint);
                            accounts.Add(rawAccount);
                        }
                    }

                    JObject account = new JObject();
                    account.Add("token", accountToken);
                    account.Add("endpoint", accountEndpoint);

                    accounts.Add(account);

                    json.Add("accounts", accounts);

                    json.Add("nightModeEnabled", MastodonAPIWrapper.sharedApiWrapper.nightModeEnabled);

                    json.Add("version", 0.3);

                    String output = json.ToString();
                    File.WriteAllText(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Capella\\settings.json", output);
                };
                worker2.RunWorkerCompleted += (sender2, e2) =>
                {
                    //System.Diagnostics.Process.Start(Application.ResourceAssembly.Location);
                    //Application.Current.Shutdown();
                    this.Close();
                };
                worker2.RunWorkerAsync();
            }
        }
示例#2
0
        public MastodonAPIWrapper()
        {
            Console.WriteLine("Initializing Capella Mastodon API Wrapper...");

            sharedApiWrapper = this;
            sharedOAuthUtils = new OAuthUtils();

            try
            {
                if (!File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Capella\\settings.json"))
                {
                    return;
                }
                String  rawJson = File.ReadAllText(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Capella\\settings.json");
                dynamic json    = JsonConvert.DeserializeObject(rawJson);

                if (json["version"] != null)
                {
                    double version = json["version"];
                    if (version < 0.3)
                    {
                        return;
                    }
                }
                else
                {
                    return;
                }

                if (json["nightModeEnabled"] != null)
                {
                    nightModeEnabled = (bool)json["nightModeEnabled"];
                }

                if (json["mutes"] != null)
                {
                    dynamic mutes = json["mutes"];
                    if (mutes["keywords"] != null)
                    {
                        JArray rawKeywords = mutes["keywords"];
                        foreach (String keyword in rawKeywords.Children())
                        {
                            keywords.Add(keyword);
                        }
                    }
                }

                dynamic accountsTokens = json["accounts"];
                if (accountsTokens == null)
                {
                    return;
                }

                if (accountsTokens.Count == 0)
                {
                    return;
                }

                Console.WriteLine("Loading Accounts...");

                accounts = new List <Account>();
                foreach (dynamic accountTokens in accountsTokens)
                {
                    Account account = new Account();
                    account.accessToken = (String)accountTokens["token"];

                    account.endpoint = (String)accountTokens["endpoint"];

                    account.myHandle = getCurrentHandle(account);

                    account.blockedIDs = new List <String>();

                    /*BackgroundWorker worker = new BackgroundWorker();
                     * worker.DoWork += (sender, e) =>
                     *  {
                     *      JObject rawData = (JObject)JsonConvert.DeserializeObject(sharedOAuthUtils.GetData("https://localhost/1.1/blocks/ids.json", "stringify_ids=true", account, true));
                     *      JArray blockedIDs = (JArray)rawData["ids"];
                     *      foreach (JValue id in blockedIDs.Children())
                     *      {
                     *          account.blockedIDs.Add((String)id);
                     *      }
                     *  };
                     * worker.RunWorkerAsync();*/

                    accounts.Add(account);
                }

                accountImages = new Dictionary <string, Image>();

                selectedAccount = accounts[0];
                String[] args = Environment.GetCommandLineArgs();
                if (args.Length > 1)
                {
                    String rawSelectedAccount = args[1];
                    int    selectedIdx;
                    if (Int32.TryParse(rawSelectedAccount, out selectedIdx))
                    {
                        if (selectedIdx < accounts.Count)
                        {
                            selectedAccount = accounts[selectedIdx];
                        }
                    }
                }
                Console.WriteLine("Capella Mastodon API Wrapper Initialized.");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }