private void ChangeSettings(object sender, EventArgs e)
        {
            Wizard w = new Wizard();

            w.ShowDialog();
        }
Exemple #2
0
        static void Main(string[] argv)
        {
            Application.SetCompatibleTextRenderingDefault(false);

            #region Make the application single instance and communitate with the main one

            /*
             * We want the application to have only one instance. When another instance is opened
             * it maight be one of the followng case:
             *      - The user tries to execute another instance by doubleclick: the application is immediately shutdown.
             *      - The user tries to send a file by right clicking on: this will launch a new instance that has to
             *              communicate with the first one and send the path of the file.
             */

            bool createdNew;

            mutex = new System.Threading.Mutex(true, "Jubilant Waffle", out createdNew);        // Tries to instanciate a named mutex and acquire the onwneship. Created new will be true if ownership has been granted
            if (!createdNew)
            {
                if (argv.Length > 0)
                {
                    /* Im assuming that if the application has be launched
                     * with at least a paramenter, it's the right click case.
                     */
                    string path = System.String.Join(" ", argv);                                // If the path of the right-clicked file contains spaces, the application will read then as several argments.
                    if (File.Exists(path) || Directory.Exists(path))                            // Just to be sure that it was a proper right-click launch.
                    {
                        Client.EnqueueMessage(path);                                            // Enstablish the communication with the main instance.
                    }
                }
                return;
            }
            #endregion
            #region Setup application
            AppDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Jubilant Waffle";
            users         = new Dictionary <String, User>();
            self          = new User("", GetMyIP());
            server        = new Server();
            client        = new Client();
            mainbox       = new Main();
            #endregion
            #region Read settings or execute wizard
            if (!File.Exists(AppDataFolder + @"\settings.ini"))
            {
                /* The settings file is store each time the applicatation is closed.
                 * If the settings files is not present, I'm assiming that it's the first launch
                 * and thus the wizard is called for the first setup
                 */
                server.DefaultPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Jubilant Waffle";
                wizard             = new Wizard();

                /* The application will hang untill the wizard is over.
                 * The wizard can terminate with confirm or cancel. Only in the first case the application will continue the execution
                 * and settings file will be store to avoid displaying the setup again at startup.
                 */
                DialogResult res = wizard.ShowDialog();
                if (res == DialogResult.Cancel)
                {
                    Environment.Exit(0);
                }
                else
                {
                    WriteSettingsFile();
                }
            }
            else
            {
                ReadSettingsFile();
            }
            #endregion
            #region Tray Icon
            trayIcon         = new NotifyIcon();
            trayIcon.Visible = true;

            /* Show a tooltip for current status when mouse is hovering */
            trayIcon.Text  = "Jubilant Waffle\nStatus: ";
            trayIcon.Text += server.Status ? "Online" : "Offline";

            /* Set icon */
            trayIcon.Icon = new System.Drawing.Icon(iconFile);

            /* Show notification */
            trayIcon.ShowBalloonTip(500, "Jubilant Waffle", "Jubilant Waffle always runs minimized into tray", ToolTipIcon.None);
            trayIcon.BalloonTipClicked += (object s, EventArgs e) => mainbox.Show();            // When ballon tips are shown, I want the main box to be shown. Ballon tips will be shown for each new incoming file.

            /*
             * Set the mouse right click menu
             */
            MenuItem[] trayContextMenuItems = new MenuItem[2];
            /* Show Exit */
            trayContextMenuItems[0]        = new MenuItem("Exit");
            trayContextMenuItems[0].Click += Exit;

            /* Show option to switch status. Text of the option change according to current status */
            trayContextMenuItems[1]        = new MenuItem();
            trayContextMenuItems[1].Text   = (server.Status) ? "Go offline" : "Go online";
            trayContextMenuItems[1].Click += ChangeStatus;

            trayIcon.ContextMenu = new ContextMenu(trayContextMenuItems);

            /* Show box on left click */
            trayIcon.MouseClick += ShowMainBox;
            #endregion
            #region Context Menu entry

            /*
             * Contex menu entries are stored under HKEY_CLASSES_ROOT for all users
             * and in HKEY_CURRENT_USER\Software\Classes for the current one.
             * Write an entry to HKEY_CLASSES_ROOT require high privileges, so
             * entry will be written in HKEY_CURRENT_USER.
             */
            /* Files */
            AddRegistryEntry(@"Software\Classes\*\shell\jubilant-waffle", "Share with Jubilant Waffle");
            AddRegistryEntry(@"Software\Classes\*\shell\jubilant-waffle\command", Application.ExecutablePath + " %1");
            AddRegistryEntry(@"Software\Classes\*\shell\jubilant-waffle\Icon", Application.ExecutablePath);
            /* Directory */
            AddRegistryEntry(@"Software\Classes\Directory\shell\jubilant-waffle", "Share with Jubilant Waffle");
            AddRegistryEntry(@"Software\Classes\Directory\shell\jubilant-waffle\command", Application.ExecutablePath + " %1");
            AddRegistryEntry(@"Software\Classes\Directory\shell\jubilant-waffle\Icon", Application.ExecutablePath);
            #endregion

            Application.Run();
        }