Example #1
0
        public static void Save(IEnumerable <HotKeyBinding> bindings)
        {
            try
            {
                var data = new StringBuilder();
                data.AppendLine(ConfigHeader);

                foreach (HotKeyBinding item in bindings)
                {
                    if (item.HotKey.IsNotEmpty())
                    {
                        data.AppendLine(item.HotKey);
                        data.AppendLine("  " + item.Name);
                        var command = string.Join("|", item.Application, item.Args).Trim();
                        if (!command.IsEmpty() && command != "|")
                        {
                            data.AppendLine("  " + command);
                        }
                        data.AppendLine();
                    }
                }

                File.WriteAllText(hotKeysMapping, data.ToString());
            }
            catch (Exception e)
            {
                Operations.MsgBox("Cannot save hot keys settings: " + e.Message, "MultiClip");
            }
        }
Example #2
0
        void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
#if DEBUG
            Operations.MsgBox(e.ExceptionObject.ToString(), "MultiClip critical error");
#else
            Debug.Assert(false, e.ToString());
#endif
        }
Example #3
0
        public static List <HotKeyBinding> LoadFrom(string mappingFile)
        {
            var result = new List <HotKeyBinding>();

            if (!File.Exists(mappingFile))
            {
                CreateDefaultMappingFile(mappingFile);
            }

            var mapping = new Dictionary <string, List <string> >();

            string currentKey = null;

            try
            {
                foreach (var line in File.ReadAllLines(mappingFile).Where(x => !x.StartsWith(";") && !x.IsEmpty()))
                {
                    if (!line.StartsWith(" ")) //hot key
                    {
                        //e.g. Ctrl+Shift+K
                        currentKey          = line.Replace("Ctrl", "Control");
                        mapping[currentKey] = new List <string>();
                    }
                    else
                    {
                        if (currentKey != null && mapping.ContainsKey(currentKey))
                        {
                            mapping[currentKey].Add(line.Trim());
                        }
                    }
                }

                foreach (string key in mapping.Keys)
                {
                    List <string> data = mapping[key];

                    string[] command = (data.Count > 1 ? data[1].Split(new[] { '|' }, 2) : new string[0]);

                    var app  = (command.FirstOrDefault() ?? "").Trim('"');
                    var args = command.Skip(1).FirstOrDefault() ?? "";

                    result.Add(new HotKeyBinding
                    {
                        HotKey        = key,
                        Name          = data[0],
                        Application   = app.StartsWith("console:") ? app.Substring("console:".Length) : app,
                        Args          = args,
                        CreateConsole = app.StartsWith("console: ")
                    });
                }
            }
            catch (Exception e)
            {
                Operations.MsgBox(e.Message, "MultiClip");
            }

            return(result);
        }
Example #4
0
        public static bool Edit(string mappingFile)
        {
            try
            {
                mappingFile = Path.GetFullPath(mappingFile);

                EnsureDefaults(mappingFile);

                var timestamp = File.GetLastWriteTimeUtc(mappingFile);
                Process.Start("notepad.exe", mappingFile).WaitForExit();
                return(timestamp != File.GetLastWriteTimeUtc(mappingFile));
            }
            catch (Exception e)
            {
                Operations.MsgBox(e.ToString(), "MultiClip.UI");
                return(false);
            }
        }
Example #5
0
        public static Dictionary <string, Action> ToKeyHandlersView(string mappingFile = null)
        {
            Dictionary <string, Action> result = new Dictionary <string, Action>();

            mappingFile = mappingFile ?? hotKeysMapping;

            if (File.Exists(mappingFile))
            {
                // var result = new StringBuilder();

                foreach (HotKeyBinding item in LoadFrom(mappingFile))
                {
                    string hotKey = item.HotKey.ToReadableHotKey();

                    var key = string.Format("{0}{2}\t - {1}", hotKey, item.Name, (hotKey.Length < 8 ? "\t" : ""));
                    if (EmbeddedHandlers.ContainsKey(item.Name)) //e.g. <MultiClip.Show>
                    {
                        result[key] = EmbeddedHandlers[item.Name];
                    }
                    else
                    {
                        string app  = item.Application;
                        string args = item.Args;

                        result[key] = () =>
                        {
                            try
                            {
                                Process.Start(app, args);
                            }
                            catch (Exception e)
                            {
                                Operations.MsgBox(e.Message, "MultiClip - " + item.Name);
                            }
                        };
                    }
                }
            }
            return(result);
        }
Example #6
0
        void Application_Startup(object sender, StartupEventArgs e)
        {
            // Debug.Assert(false);

            if (Environment.GetCommandLineArgs().Contains("-kill"))
            {
                var runningGui = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().Location))
                                 .Where(p => p.Id != Process.GetCurrentProcess().Id);

                foreach (var server in runningGui)
                {
                    try
                    {
                        server.Kill();
                    }
                    catch { }
                }
                ClipboardMonitor.KillAllServers();

                Shutdown();
            }
            else
            {
                Log.WriteLine($"=============== Started =================");
                Log.WriteLine(Assembly.GetExecutingAssembly().Location);
                //new SettingsView().ShowDialog();return;
                //IMPORTANT: Do not release the mutex. OS will release the mutex on app exit automatically.
                mutex = new Mutex(true, "multiclip.history");
                if (mutex.WaitOne(0))
                {
                    StartApp();
                }
                else
                {
                    Operations.MsgBox("Another instance of the application is already running.", "MultiClip");
                    Shutdown();
                }
            }
        }
Example #7
0
        public static void Bind(HotKeys engine, ToolStripMenuItem rootMenu, string mappingFile)
        {
            if (engine.Started)
            {
                EnsureDefaults(mappingFile);

                if (File.Exists(mappingFile))
                {
                    TrayIcon.InvokeMenu.DropDownItems.Clear();

                    foreach (HotKeyBinding item in LoadFrom(mappingFile))
                    {
                        try
                        {
                            string[] tokens = item.HotKey.Split('+').Select(x => x.Trim()).ToArray();

                            Keys key = tokens.Last().ToKeys();

                            Modifiers modifiers = tokens.Take(tokens.Length - 1)
                                                  .ToModifiers();

                            var handlerName = item.Name;

                            Action handler;
                            if (EmbeddedHandlers.ContainsKey(handlerName)) //<MultiClip.Show>
                            {
                                handler = EmbeddedHandlers[handlerName];
                            }
                            else
                            {
                                string app  = item.Application;
                                string args = item.Args;

                                handler = () =>
                                {
                                    try
                                    {
                                        if (!item.CreateConsole)
                                        {
                                            var info = new ProcessStartInfo
                                            {
                                                FileName               = app,
                                                Arguments              = args,
                                                UseShellExecute        = false,
                                                RedirectStandardOutput = true,
                                                CreateNoWindow         = true
                                            };
                                            Process.Start(info);
                                        }
                                        else
                                        {
                                            Process.Start(app, args);
                                        }
                                    }
                                    catch (Exception e)
                                    {
                                        Operations.MsgBox(e.Message, "MultiClip - " + handlerName);
                                    }
                                };
                            }

                            rootMenu.DropDownItems.Add(handlerName, null, (s, e) => handler());
                            engine.Bind(modifiers, key, handler);
                        }
                        catch { }
                    }
                }
            }
        }
Example #8
0
 public void About()
 {
     Operations.MsgBox("Multi-item clipboard buffer.\n\nVersion: " + Assembly.GetExecutingAssembly().GetName().Version + "\nCopyright © Oleg Shilo 2015", "Multiclip");
 }