Exemplo n.º 1
0
        public GinApplicationContext()
        {
            _trayIcon = new NotifyIcon
            {
                Visible = true,
                Icon    = Resources.gin_icon_desaturated
            };

            ServiceClient = new GinServiceClient(new InstanceContext(this));
            var saveFilePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
                               @"\g-node\GinWindowsClient";

            if (!Directory.Exists(saveFilePath))
            {
                Directory.CreateDirectory(saveFilePath);
            }

            #region Environment Variables

            //Tell the service to use the current users' AppData folders for logging and config data
            ServiceClient.SetEnvironmentVariables(
                Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\g-node\gin\",
                Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\g-node\gin\");

            #endregion

            #region Login

            if (!UserCredentials.Load())
            {
                var getUserCreds = new MetroGetUserCredentialsDlg(this);
                var result       = getUserCreds.ShowDialog(); //The Dialog will log us in and save the user credentials

                if (result == DialogResult.Cancel)
                {
                    Exit(this, EventArgs.Empty);
                }
            }
            else if (!ServiceClient.Login(UserCredentials.Instance.Username, UserCredentials.Instance.Password))
            {
                MessageBox.Show(Resources.GinApplicationContext_Error_while_trying_to_log_in_to_GIN,
                                Resources.GinApplicationContext_Gin_Client_Error,
                                MessageBoxButtons.OK, MessageBoxIcon.Error);

                var getUserCreds = new MetroGetUserCredentialsDlg(this);
                var result       = getUserCreds.ShowDialog(); //The Dialog will log us in and save the user credentials

                if (result == DialogResult.Cancel)
                {
                    Exit(this, EventArgs.Empty);
                }
            }

            UserCredentials.Save();

            #endregion

            #region Read options

            if (!GlobalOptions.Load())
            {
                var optionsDlg = new MetroOptionsDlg(this, MetroOptionsDlg.Page.GlobalOptions);
                var result     = optionsDlg.ShowDialog();

                if (result == DialogResult.Cancel)
                {
                    Exit(this, EventArgs.Empty);
                }
            }

            if (GlobalOptions.Instance.RepositoryUpdateInterval > 0)
            {
                _updateIntervalTimer =
                    new Timer(GlobalOptions.Instance.RepositoryUpdateInterval * 1000 * 60)
                {
                    AutoReset = true
                };
                _updateIntervalTimer.Elapsed += (sender, args) => { ServiceClient.DownloadAllUpdateInfo(); };
            }

            GlobalOptions.Save();

            #endregion

            #region Set up repositories

            if (File.Exists(saveFilePath + @"\SavedRepositories.json"))
            {
                try
                {
                    var text  = File.OpenText(saveFilePath + @"\SavedRepositories.json").ReadToEnd();
                    var repos = JsonConvert.DeserializeObject <GinRepositoryData[]>(text);

                    foreach (var repo in repos)
                    {
                        ServiceClient.AddRepository(repo.PhysicalDirectory.FullName, repo.Mountpoint.FullName,
                                                    repo.Name,
                                                    repo.Address,
                                                    GlobalOptions.Instance.RepositoryCheckoutOption ==
                                                    GlobalOptions.CheckoutOption.FullCheckout, false);
                    }
                }
                catch
                {
                }
            }
            else
            {
                ManageRepositoriesMenuItemHandler(null, EventArgs.Empty);
            }

            #endregion


            _trayIcon.DoubleClick += _trayIcon_DoubleClick;
            _trayIcon.ContextMenu  = new ContextMenu(BuildContextMenu());
            _trayIcon.Icon         = Resources.gin_icon;
            _updateIntervalTimer?.Start();
        }
Exemplo n.º 2
0
        public GinApplicationContext()
        {
            _trayIcon = new NotifyIcon
            {
                Visible = true,
                Icon    = Resources.gin_icon_desaturated
            };

            var service = new GinClientWindowsService(_trayIcon);

            _serviceThread = new Thread(service.Start);
            _serviceThread.Start();

            SystemEvents.SessionEnded += SystemEvents_SessionEnded;

            var myBinding = new WSDualHttpBinding
            {
                ClientBaseAddress      = new Uri(@"http://*****:*****@"\g-node\WinGIN";

            if (!Directory.Exists(saveFilePath))
            {
                Directory.CreateDirectory(saveFilePath);
            }

            #region Environment Variables

            //Tell the service to use the current users' AppData folders for logging and config data
            ServiceClient.SetEnvironmentVariables(
                Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\g-node\gin\",
                Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\g-node\gin\");

            #endregion

            #region Login

            if (!UserCredentials.Load())
            {
                var getUserCreds = new MetroGetUserCredentialsDlg(this);
                var result       = getUserCreds.ShowDialog(); //The Dialog will log us in and save the user credentials

                if (result == DialogResult.Cancel)
                {
                    Exit(this, EventArgs.Empty);
                    return;
                }
            }
            else
            {
                var servString = ServiceClient.GetServers();
                var ServerDic  = JsonConvert.DeserializeObject <Dictionary <string, ServerConf> >(servString);
                foreach (var server in ServerDic)
                {
                    ///search for login for server
                    var selectedLogin = UserCredentials.Instance.loginList.Find(x => x.Server == server.Key);
                    if (selectedLogin != null)
                    {
                        ///try login for servers
                        if (!ServiceClient.Login(selectedLogin.Username, selectedLogin.Password, selectedLogin.Server))
                        {
                            if (server.Value.Default)
                            {
                                ///if login fails for default server, try to get new login info
                                MessageBox.Show(Resources.GinApplicationContext_Error_while_trying_to_log_in_to_GIN,
                                                Resources.GinApplicationContext_Gin_Client_Error,
                                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                                var getUserCreds = new MetroGetUserCredentialsDlg(this);
                                var result       = getUserCreds.ShowDialog(); ///The Dialog will log us in and save the user credentials
                                if (result == DialogResult.Cancel)
                                {
                                    Exit(this, EventArgs.Empty);
                                    return;
                                }
                            }
                        }
                    }
                }
            }

            UserCredentials.Save();

            #endregion

            #region Read options

            if (!GlobalOptions.Load())
            {
                var optionsDlg = new MetroOptionsDlg(this, MetroOptionsDlg.Page.GlobalOptions);
                optionsDlg.RepoListingChanged += (o, args) => { _trayIcon.ContextMenu = new ContextMenu(BuildContextMenu()); };
                var result = optionsDlg.ShowDialog();

                if (result == DialogResult.Cancel)
                {
                    Exit(this, EventArgs.Empty);
                }
            }

            if (GlobalOptions.Instance.RepositoryUpdateInterval > 0)
            {
                _updateIntervalTimer =
                    new Timer(GlobalOptions.Instance.RepositoryUpdateInterval * 1000 * 60)
                {
                    AutoReset = true
                };
                _updateIntervalTimer.Elapsed += (sender, args) => { ServiceClient.DownloadAllUpdateInfo(); };
            }
            else
            {
                ///no automatic updates; check the service every 45 minutes to prevent service time out
                _updateIntervalTimer =
                    new Timer(45 * 1000 * 60)
                {
                    AutoReset = true
                };
                _updateIntervalTimer.Elapsed += (sender, args) => { ServiceClient.IsAlive(); };
            }

            GlobalOptions.Save();

            #endregion

            #region Set up repositories

            if (File.Exists(saveFilePath + @"\SavedRepositories.json"))
            {
                try
                {
                    using (var saveFile = File.OpenText(saveFilePath + @"\SavedRepositories.json"))
                    {
                        var text  = saveFile.ReadToEnd();
                        var repos = JsonConvert.DeserializeObject <GinRepositoryData[]>(text);

                        foreach (var repo in repos)
                        {
                            ServiceClient.AddRepository(repo.PhysicalDirectory.FullName, repo.Mountpoint.FullName,
                                                        repo.Name,
                                                        repo.Address,
                                                        GlobalOptions.Instance.RepositoryCheckoutOption ==
                                                        GlobalOptions.CheckoutOption.FullCheckout, false);
                        }
                    }
                }
                catch
                {
                }
            }
            else
            {
                ManageRepositoriesMenuItemHandler(null, EventArgs.Empty);
            }

            #endregion


            _trayIcon.DoubleClick += _trayIcon_DoubleClick;
            _trayIcon.ContextMenu  = new ContextMenu(BuildContextMenu());
            _trayIcon.Icon         = Resources.gin_icon;
            _updateIntervalTimer?.Start();
        }