Ejemplo n.º 1
0
        public void setLabelId(string labelid)
        {
            label_id = labelid;
            iniParser parser = new iniParser();

            parser.IniParser(iniPath);
            parser.AddSetting("Setup", "label", label_id);
            parser.SaveSettings();
        }
Ejemplo n.º 2
0
        private void service_oauth()
        {
            // MessageBox.Show(RefreshKey_saved);
            if (RefreshKey_saved != "")
            {
                bool result = getAccessToken("aa");
                if (result == true)
                {
                    myTabControl.SelectedIndex = 0;
                    // set focus to AutoCompleteBox
                    AutoCompleteBox.Focusable = true;
                    Keyboard.Focus(AutoCompleteBox);
                    return;
                }
                else
                {
                    iniParser parser       = new iniParser();
                    String    appStartPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
                    parser.IniParser(iniPath);
                    parser.AddSetting("Setup", "refreshkey", "");
                    parser.SaveSettings();
                    RefreshKey_saved = "";
                    if (initialState == true)
                    {
                        // no switch screen
                        myTabControl.SelectedIndex = 0;
                        return;
                    }
                    myTabControl.SelectedIndex = 1;              // access token expire
                }
                // set focus to AutoCompleteBox
                AutoCompleteBox.Focusable = true;
                Keyboard.Focus(AutoCompleteBox);
            }

            //-------
            consumer = new NativeApplicationClient(server, clientID, clientSecret);
            Uri userAuthorizationUri = consumer.RequestUserAuthorization(scope2);
            //label_msg.DataContext = GoogleScope.ImapAndSmtp.Name;  //GoogleScope.EmailAddressScope.Name
            string address =
                "https://accounts.google.com/o/oauth2/auth" +
                "?client_id=" + clientID +
                "&response_type=code" +
                "&access_type=offline" +
                "&scope=" + GoogleScope.ContactsScope.Name +  //GoogleScope.ImapAndSmtp.Name +
                "&redirect_uri=" + "urn:ietf:wg:oauth:2.0:oob";

            wb1.Navigate(new Uri(address, UriKind.Absolute));
            wb1.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(wb1_LoadCompleted);
        }
Ejemplo n.º 3
0
        private void wb1_LoadCompleted(object sender, NavigationEventArgs e)
        {
            dynamic doc      = wb1.Document;
            var     htmlText = doc.documentElement.InnerHtml;
            string  _str     = htmlText;

            _str = _str.Replace(@"\\", "");
            int i0 = _str.IndexOf("<title>");

            i0 = _str.IndexOf("Success code=", i0 + 1);

            if (i0 == -1)
            {
                i0 = _str.IndexOf("Denied error", i0 + 1);
                if (i0 >= 0)
                {
                    myTabControl.SelectedIndex = 0;
                    image_gmail.Visibility     = System.Windows.Visibility.Collapsed;
                    connected_gmail.Visibility = image_gmail.Visibility;
                    button_import.Visibility   = Visibility.Visible;
                    // user say no, just return to input page
                    // myTabControl.SelectedIndex = 0;
                }
                return;       // not the last pagr or error
            }
            int    i1    = _str.IndexOf("</title>", i0 + 1);
            string token = _str.Substring(i0 + 13, i1 - i0 - 13);

            myTabControl.SelectedIndex = 0;  // $$$

            bool result = getAccessToken(token);

            if (result == false)
            {
                connected_gmail.Visibility = Visibility.Collapsed;
                image_gmail.Visibility     = Visibility.Collapsed;
                // clear Setup refresh key
                iniParser parser = new iniParser();
                // String appStartPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
                parser.IniParser(iniPath);
                parser.AddSetting("Setup", "refreshkey", "");
                parser.SaveSettings();
            }
            else
            {
                connected_gmail.Visibility = Visibility.Visible;
                image_gmail.Visibility     = Visibility.Visible;
            }
        }
Ejemplo n.º 4
0
        public void setRun()
        {
            // get ini for refreshkey
            iniParser parser       = new iniParser();
            String    appStartPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);

            parser.IniParser(iniPath);  //appStartPath + @"\sharefavorite.ini");
            RefreshKey_saved = parser.GetSetting("Setup", "refreshkey");
            //
            AutoCompleteBox.Focusable = true;
            Keyboard.Focus(AutoCompleteBox);
            //
            if (RefreshKey_saved == "")
            {
                myTabControl.SelectedIndex = 1;
            }
            service_oauth();
        }
Ejemplo n.º 5
0
        public void setRun_button()
        {
            // add state: if initialState = true; then first time link to google no show screen if failed

            // get ini for refreshkey
            iniParser parser = new iniParser();

            if (File.Exists(iniPath))
            {
                parser.IniParser(iniPath);                  //appStartPath + @"\sharefavorite.ini");
                RefreshKey_saved = parser.GetSetting("Setup", "refreshkey");
                //
                RefreshKey_real = parser.GetSetting("Setup", "refreshkey_real");
                if (RefreshKey_real != "")
                {
                    bool result = get_accesstokenfromrefreshtoken();
                    if (result == true)                                  // exchange refresh token with refresh token
                    {
                        RefreshKey_saved = access_token;
                    }
                }
                AutoCompleteBox.Focusable = true;
                Keyboard.Focus(AutoCompleteBox);
                //
                if (RefreshKey_saved == "")
                {
                    if (initialState == true)
                    {
                        myTabControl.SelectedIndex = 0;
                    }
                    else
                    {
                        myTabControl.SelectedIndex = 1;
                    }
                }
                service_oauth();
            }
        }
Ejemplo n.º 6
0
        private bool getAccessToken(string authCode)
        {
            bool   getSuccess  = false;
            string accessToken = "";

            if (RefreshKey_saved == "")
            {
                consumer.ClientCredentialApplicator =
                    ClientCredentialApplicator.PostParameter(clientSecret);

                IAuthorizationState grantedAccess1 = consumer.ProcessUserAuthorization(authCode);

                accessToken     = grantedAccess1.AccessToken;
                RefreshKey_real = grantedAccess1.RefreshToken;

                // save key
                iniParser parser = new iniParser();

                parser.IniParser(iniPath);
                parser.AddSetting("Setup", "refreshkey", grantedAccess1.AccessToken);
                parser.AddSetting("Setup", "refreshkey_real", grantedAccess1.RefreshToken);
                parser.SaveSettings();
                myTabControl.SelectedIndex = 0;
            }
            else
            {
                accessToken = RefreshKey_saved;
                myTabControl.SelectedIndex = 0;
            }
            try
            {
                GoogleApi api = new GoogleApi(accessToken);

                // string user = "******"; // api.GetEmail();
                // GoogleApi api = new GoogleApi(accessToken);

                XmlDocument         contacts = api.GetContacts();
                XmlNamespaceManager nsmgr    = new XmlNamespaceManager(contacts.NameTable);
                //

                XmlNodeList _title = contacts.GetElementsByTagName("title");
                string      temp   = _title.Item(0).InnerText;
                temp        = temp.Replace("'s Contacts", "");
                sender_name = temp;
                if (textbox_name.Text == "")
                {
                    textbox_name.Text = temp;
                }

                nsmgr.AddNamespace("gd", "http://schemas.google.com/g/2005");
                nsmgr.AddNamespace("a", "http://www.w3.org/2005/Atom");
                emailCount = 0;

                foreach (XmlNode contact in contacts.GetElementsByTagName("entry"))
                {
                    XmlNode title = contact.SelectSingleNode("a:title", nsmgr);
                    XmlNode email = contact.SelectSingleNode("gd:email", nsmgr);

                    // Console.WriteLine("{0}: {1}",title.InnerText, email.Attributes["address"].Value);
                    if (email != null)
                    {
                        title_arr.Add(title.InnerText);
                        gmail_arr.Add(email.Attributes["address"].Value);
                        emailCount++;
                    }
                }
                getSuccess = true;
                button_import.Visibility   = Visibility.Collapsed;
                image_gmail.Visibility     = System.Windows.Visibility.Visible;
                connected_gmail.Visibility = image_gmail.Visibility;
                myTabControl.SelectedIndex = 0;
            }
            catch (Exception err)
            {
                Console.WriteLine("Error: " + err.Message);
                getSuccess = false;
                return(getSuccess);
            }
            // everything is good, goto input : autocomplete
            contactData.inc(emailCount);
            int i = 0;

            foreach (string emailAddress in gmail_arr)
            {
                contactData.States.SetValue(emailAddress, i);
                i++;
            }
            int where = contactData.States.Length;
            //
            AutoCompleteBox.ItemsSource = contactData.States;
            //email_list.Items.Clear();
            label_invalid.Visibility = Visibility.Collapsed;
            // myTabControl.SelectedIndex = 0;
            return(getSuccess);
        }
Ejemplo n.º 7
0
        private bool getAccessToken(string authCode)
        {
            bool   getSuccess  = false;
            string accessToken = "";

            if (RefreshKey_saved == "")
            {
                consumer.ClientCredentialApplicator =
                    ClientCredentialApplicator.PostParameter(clientSecret);

                IAuthorizationState grantedAccess1 = consumer.ProcessUserAuthorization(authCode);
                bool result = consumer.RefreshAuthorization(grantedAccess1, TimeSpan.FromHours(10));

                accessToken = grantedAccess1.AccessToken;

                // save key
                iniParser parser       = new iniParser();
                String    appStartPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
                parser.IniParser(iniPath);
                string _r = grantedAccess1.AccessToken; //.RefreshToken;
                parser.AddSetting("Setup", "refreshkey", _r);
                parser.SaveSettings();
                myTabControl.SelectedIndex = 0;
            }
            else
            {
                // change code immediately
                //  consumer.RefreshAuthorization(grantedAccess1, TimeSpan.FromDays(30));
                //accessToken = grantedAccess1.AccessToken;
                accessToken = RefreshKey_saved;
                myTabControl.SelectedIndex = 0;
            }
            try
            {
                GoogleApi api = new GoogleApi(accessToken);


                // string user = "******"; // api.GetEmail();
                // GoogleApi api = new GoogleApi(accessToken);

                XmlDocument contacts = api.GetContacts();

                XmlNamespaceManager nsmgr = new XmlNamespaceManager(contacts.NameTable);
                nsmgr.AddNamespace("gd", "http://schemas.google.com/g/2005");
                nsmgr.AddNamespace("a", "http://www.w3.org/2005/Atom");
                emailCount = 0;
                foreach (XmlNode contact in contacts.GetElementsByTagName("entry"))
                {
                    XmlNode title = contact.SelectSingleNode("a:title", nsmgr);
                    XmlNode email = contact.SelectSingleNode("gd:email", nsmgr);

                    Console.WriteLine("{0}: {1}",
                                      title.InnerText,
                                      email.Attributes["address"].Value);
                    mail_arr.Add(email.Attributes["address"].Value);
                    emailCount++;
                    //   listbox1.Items.Add(title.InnerText + " , " + email.Attributes["address"].Value);
                }
                getSuccess = true;
            }
            catch (Exception err)
            {
                // MessageBox.Show("error: " + err.Message);
                Console.WriteLine("Error: " + err.Message);
                getSuccess = false;
                return(getSuccess);
            }
            // everything is good, goto input : autocomplete
            contactData.inc(emailCount);
            int i = 0;

            foreach (string emailAddress in mail_arr)
            {
                contactData.States.SetValue(emailAddress, i);
                i++;
            }
            int where = contactData.States.Length;
            //
            AutoCompleteBox.ItemsSource = contactData.States;
            email_list.Items.Clear();
            label_invalid.Visibility = Visibility.Collapsed;
            // myTabControl.SelectedIndex = 0;
            return(getSuccess);

//------------------------
            try
            {
                // get inbox mail content
                // #region get InBox
                string user = "******"; // api.GetEmail();
                using (Imap imap = new Imap())
                {
                    imap.ConnectSSL("imap.gmail.com");
                    imap.LoginOAUTH2(user, accessToken);

                    imap.SelectInbox();
                    List <long> uids = imap.Search(Flag.Unseen);

                    foreach (long uid in uids)
                    {
                        string eml   = imap.GetMessageByUID(uid);
                        IMail  email = new MailBuilder().CreateFromEml(eml);

                        // listbox1.Items.Add(email.From);
                    }
                    imap.Close();
                }
            }
            catch (Exception err)
            {
                MessageBox.Show("error: " + err.Message);
            }
        }
Ejemplo n.º 8
0
        private void wb1_LoadCompleted(object sender, NavigationEventArgs e)
        {
            dynamic doc = wb1.Document;
            var htmlText = doc.documentElement.InnerHtml;
            string _str = htmlText;
            _str = _str.Replace(@"\\", "");
            int i0 = _str.IndexOf("<title>");
            i0 = _str.IndexOf("Success code=", i0 + 1);

            if (i0 == -1)
            {
                i0 = _str.IndexOf("Denied error", i0 + 1);
                if (i0 >= 0)
                {
                    // user say no, just return to input page
                   // myTabControl.SelectedIndex = 0;
                }
                return;       // not the last pagr or error
            }
            int i1 = _str.IndexOf("</title>", i0 + 1);
            string token = _str.Substring(i0 + 13, i1 - i0 - 13);

            myTabControl.SelectedIndex = 0;  // $$$
            bool result=getAccessToken(token);
            if (result == false)
            {
                connected_gmail.Visibility = Visibility.Collapsed;
                image_gmail.Visibility = Visibility.Collapsed;
                // clear Setup refresh key
                iniParser parser = new iniParser();
                String appStartPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
                parser.IniParser(iniPath);
                parser.AddSetting("Setup", "refreshkey", "");
                parser.SaveSettings();

            }
            else
            {
                connected_gmail.Visibility = Visibility.Visible;
                image_gmail.Visibility = Visibility.Visible;
            }
        }
Ejemplo n.º 9
0
        private void service_oauth()
        {
            if (RefreshKey_saved != "")
            {
               bool result =getAccessToken("aa");
               if (result == true)
               {
                  myTabControl.SelectedIndex=0;
                   // set focus to AutoCompleteBox
                  AutoCompleteBox.Focusable = true;
                  Keyboard.Focus(AutoCompleteBox);
                   return;
               }
               else
               {
                   iniParser parser = new iniParser();
                   String appStartPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
                   parser.IniParser(iniPath);
                   parser.AddSetting("Setup", "refreshkey", "");
                   parser.SaveSettings();
                   RefreshKey_saved = "";
                   myTabControl.SelectedIndex = 1;              // access token expire
               }
               // set focus to AutoCompleteBox
               AutoCompleteBox.Focusable = true;
               Keyboard.Focus(AutoCompleteBox);
                // change code immediately
                //string _accesstoken= consumer.GetScopedAccessToken(RefreshKey_saved,_set1);
                //consumer.RefreshAuthorization(grantedAccess, TimeSpan.FromMinutes(20));
                //accessToken = grantedAccess.AccessToken;
            }

            //-------
            consumer = new NativeApplicationClient(server, clientID, clientSecret);
            Uri userAuthorizationUri = consumer.RequestUserAuthorization(scope2);
            //label_msg.DataContext = GoogleScope.ImapAndSmtp.Name;  //GoogleScope.EmailAddressScope.Name
            string address =
                "https://accounts.google.com/o/oauth2/auth" +
                "?client_id=" + clientID +
                "&response_type=code" +
                "&access_type=offline" +
                "&scope=" + GoogleScope.ContactsScope.Name +  //GoogleScope.ImapAndSmtp.Name +
                "&redirect_uri=" + "urn:ietf:wg:oauth:2.0:oob";

            wb1.Navigate(new Uri(address, UriKind.Absolute));
            wb1.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(wb1_LoadCompleted);
        }
Ejemplo n.º 10
0
        private bool getAccessToken(string authCode)
        {
            bool getSuccess = false;
            string  accessToken="";
            if(RefreshKey_saved == "")
            {
            consumer.ClientCredentialApplicator =
                     ClientCredentialApplicator.PostParameter(clientSecret);

            IAuthorizationState grantedAccess1 = consumer.ProcessUserAuthorization(authCode);
            bool result=consumer.RefreshAuthorization(grantedAccess1, TimeSpan.FromHours(10));

            accessToken = grantedAccess1.AccessToken;

            // save key
            iniParser parser = new iniParser();
            String appStartPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
            parser.IniParser(iniPath);
            string _r=grantedAccess1.AccessToken;   //.RefreshToken;
            parser.AddSetting("Setup", "refreshkey", _r);
            parser.SaveSettings();
            myTabControl.SelectedIndex = 0;
            }
            else {
            // change code immediately
               //  consumer.RefreshAuthorization(grantedAccess1, TimeSpan.FromDays(30));
            //accessToken = grantedAccess1.AccessToken;
                 accessToken=RefreshKey_saved;
                 myTabControl.SelectedIndex = 0;
            }
                try
            {

                GoogleApi api = new GoogleApi(accessToken);

                // string user = "******"; // api.GetEmail();
                // GoogleApi api = new GoogleApi(accessToken);

                XmlDocument contacts = api.GetContacts();

                XmlNamespaceManager nsmgr = new XmlNamespaceManager(contacts.NameTable);
                nsmgr.AddNamespace("gd", "http://schemas.google.com/g/2005");
                nsmgr.AddNamespace("a", "http://www.w3.org/2005/Atom");
                emailCount = 0;
                foreach (XmlNode contact in contacts.GetElementsByTagName("entry"))
                {
                    XmlNode title = contact.SelectSingleNode("a:title", nsmgr);
                    XmlNode email = contact.SelectSingleNode("gd:email", nsmgr);

                    Console.WriteLine("{0}: {1}",
                        title.InnerText,
                        email.Attributes["address"].Value);
                    mail_arr.Add(email.Attributes["address"].Value);
                    emailCount++;
                 //   listbox1.Items.Add(title.InnerText + " , " + email.Attributes["address"].Value);
                }
                getSuccess = true;
            }
            catch (Exception err)
            {
               // MessageBox.Show("error: " + err.Message);
                Console.WriteLine("Error: " + err.Message);
                getSuccess = false;
                return getSuccess;
            }
            // everything is good, goto input : autocomplete
            contactData.inc(emailCount);
            int i = 0;
            foreach (string emailAddress in mail_arr)
            {
                contactData.States.SetValue(emailAddress, i);
                i++;
            }
            int where = contactData.States.Length;
            //
            AutoCompleteBox.ItemsSource = contactData.States;
            email_list.Items.Clear();
            label_invalid.Visibility = Visibility.Collapsed;
               // myTabControl.SelectedIndex = 0;
            return getSuccess;

            //------------------------
            try
            {
                // get inbox mail content
               // #region get InBox
                string user = "******"; // api.GetEmail();
                using (Imap imap = new Imap())
                {
                    imap.ConnectSSL("imap.gmail.com");
                    imap.LoginOAUTH2(user, accessToken);

                    imap.SelectInbox();
                    List<long> uids = imap.Search(Flag.Unseen);

                    foreach (long uid in uids)
                    {
                        string eml = imap.GetMessageByUID(uid);
                        IMail email = new MailBuilder().CreateFromEml(eml);

                       // listbox1.Items.Add(email.From);
                    }
                    imap.Close();
                }
            }
            catch (Exception err)
            {
                MessageBox.Show("error: " + err.Message);
            }
        }
Ejemplo n.º 11
0
 public void setRun()
 {
     // get ini for refreshkey
     iniParser parser = new iniParser();
     String appStartPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
     parser.IniParser(iniPath);  //appStartPath + @"\sharefavorite.ini");
     RefreshKey_saved = parser.GetSetting("Setup", "refreshkey");
     //
     AutoCompleteBox.Focusable = true;
     Keyboard.Focus(AutoCompleteBox);
     //
     if (RefreshKey_saved == "")
     {
         myTabControl.SelectedIndex = 1;
     }
     service_oauth();
 }