Beispiel #1
0
 private void AccountMailAddress_MouseClick(object sender, MouseEventArgs e)
 {
     if (e.Button == MouseButtons.Right)
     {
         DialogResult Res = MessageBox.Show("Delete this account from " + ProgramInfo.App.Name + "?", "Delete account?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
         if (Res == DialogResult.Yes)
         {
             try
             {
                 String PathImport = Path.Combine(Operations.GlobalVarriable.UserToken, MailAdress);
                 if (Directory.Exists(PathImport))
                 {
                     Dispose();
                     Directory.Delete(PathImport, true);
                     Operations.GlobalVarriable.MailsAddressList.Remove(MailAdress);
                     Operations.Fast.UpdateAccountsList();
                     SuccessBox.Show("Successfully deleted!");
                 }
             }
             catch (Exception)
             {
                 FailBox.Show();
             }
         }
     }
     else
     {
         if (UserClicked != null)
         {
             UserClicked(sender, e);
         }
         /////////
         String PathImport = System.IO.Path.Combine(Operations.GlobalVarriable.UserToken, MailAdress);
         if (System.IO.Directory.Exists(PathImport))
         {
             Operations.GlobalVarriable.ListOfActiveAccounts.Add(MailAdress);
             WorkSpace.WorkSpace WorkSpace = new WorkSpace.WorkSpace((String.IsNullOrEmpty(DisplayName) ? MailAdress : DisplayName)
                                                                     , Avatar, MailAdress, PathImport);
             WorkSpace.ShowDialog();
         }
         /////////
     }
 }
Beispiel #2
0
        // Function to get token
        public bool Get()
        {
            // Convert this string to Stream to read at Gmail API
            MemoryStream StreamJson = new MemoryStream(Encoding.ASCII.GetBytes(ConfigGmail.JsonContent_Gmail));

            try
            {
                // Create temp folder to save user token after request
                string NewUserFolder = string.Empty;
                NewUserFolder = Path.Combine(GlobalVarriable.UserToken, Fast.CreateNewRandomFolderName());
                // If this folder existed then auto generate new folder path
                while (File.Exists(NewUserFolder))
                {
                    NewUserFolder = Path.Combine(GlobalVarriable.UserToken, Fast.CreateNewRandomFolderName());
                }
                // Set value for UserCredential (Credential: Chứng chỉ)
                UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(StreamJson).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(NewUserFolder, true)).Result;
                // Create Gmail API service. (servis: Dịch vụ)
                var service = new GmailService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName       = ApplicationName
                });
                // Define parameters of request
                UsersResource.GetProfileRequest UserProfile = service.Users.GetProfile("me");
                // User detail
                Profile NewAccount    = UserProfile.Execute();
                string  UserEmail     = NewAccount.EmailAddress;
                string  MessagesTotal = NewAccount.MessagesTotal.ToString();
                string  ThreadsTotal  = NewAccount.ThreadsTotal.ToString();
                // Get user avatar form uri, covert to bitmap and save to User Space
                GmailAccountInfo.SendRequest(UserEmail); // sendrequest
                bool UpdateAccount = false;
                if (Directory.Exists(Path.Combine(Path.GetDirectoryName(NewUserFolder), UserEmail)))
                {
                    UpdateAccount = true;
                    Directory.Delete(Path.Combine(Path.GetDirectoryName(NewUserFolder), UserEmail), true);
                }
                // Change Folder random name to user's mail address name
                Directory.Move(NewUserFolder, Path.Combine(Path.GetDirectoryName(NewUserFolder), UserEmail));
                // Update NewUserFolder
                NewUserFolder = Path.Combine(Path.GetDirectoryName(NewUserFolder), UserEmail);
                GmailAccountInfo.get.Avatar.Save(Path.Combine(NewUserFolder, "Avatar.jpg"), ImageFormat.Jpeg);
                string UserContentGenerate =
                    Convert.ToBase64String(Encoding.UTF8.GetBytes(GmailAccountInfo.get.DisplayName)) // DisplayName
                    + "|" +
                    ThreadsTotal.ToString()                                                          // Total Thread
                    + "|" +
                    MessagesTotal.ToString();                                                        // Total Messages
                FileStream file     = new FileStream(Path.Combine(NewUserFolder, "Old.info"), FileMode.OpenOrCreate);
                byte[]     ForWrite = Encoding.ASCII.GetBytes(UserContentGenerate);
                file.Write(ForWrite, 0, ForWrite.Length);
                file.Close();
                // Should be add a case that if this Email had had before!
                //
                //
                // If Mail address had had before -> Out
                if (GlobalVarriable.MailsAddressList.IndexOf(UserEmail) != -1)
                {
                    return(false);
                }
                // Add this email to MailsAddressList
                GlobalVarriable.MailsAddressList.Add(UserEmail);
                // Update this account to List
                Fast.UpdateAccountsList();
                SuccessBox.Show("Successfully added!");
                // End of connections
                service.Dispose();
                return(true);
            }
            catch (Exception e)
            {
                // If have any bugs
                Console.WriteLine(e);
                FailBox.Show();
                return(false);
            }
        }