public static bool openCommandContainingFolder(LnzLaunchCommand cmd) { //shouldn't throw an exception, just return false upon error string sTarget = getBaseFileFromCommand(cmd); if (sTarget==null) return false; showFileInExplorer(sTarget); return true; }
//returns null if file doesn't exist public static string getBaseFileFromCommand(LnzLaunchCommand cmd) { //use heuristics to see if we can find containing the file of the command //we won't be able to replace %clip% or %1% string sTarget = cmd.sCmdTarget; sTarget = sTarget.Split('\t')[0]; if (!File.Exists(sTarget)) return null; return sTarget; }
public void lnzsetIcon(LnzLaunchCommand cmd) { if (cmd== null) {switchicon(null); return;} string sFilename = (cmd.sCustomIcon!=null) ? cmd.sCustomIcon : LaunchFiles.getBaseFileFromCommand(cmd); if (sFilename== null) {switchicon(null); return;} Icon icn = GetFileIcon.Getfileicon(sFilename); if (icn == null) { switchicon(null); return; } this.switchicon( icn.ToBitmap()); icn.Dispose(); //it has been copied to the bitmap, so this can be freed. }
//TODO: catch exceptions. try giving this really bad data/nonexistent files, see where breaks public static string runCommand(string textInBox, LnzLaunchCommand cmd) { //be sure to escape " with \" . don't need to escape \ with \\. string sTarget = cmd.sCmdTarget; Debug.Assert(!textInBox.Contains("\t")); //that'd mess things up since we split on tab. if (sTarget.Contains("%clip%")) { string strClip = ""; IDataObject data = Clipboard.GetDataObject(); if (data!=null && data.GetDataPresent(DataFormats.Text)) strClip = (string)data.GetData(DataFormats.Text, true); sTarget = sTarget.Replace("%clip%", strClip); } if (sTarget.Contains("%1%")) { string strArgs = ""; //arguments passed to command if (textInBox.StartsWith(cmd.sCmdShortcut)) //must be an actual match to pass args, otherwise "" is passed { if (cmd.sCmdShortcut == textInBox) strArgs = ""; else strArgs = textInBox.Substring(cmd.sCmdShortcut.Length).Trim(); sTarget = sTarget.Replace("%1%", strArgs); } else sTarget = sTarget.Replace("%1%", ""); } // the %.% has already been replaced. And, all of the custom constants have been replaced Debug.Assert(!sTarget.Contains("%")); //should have done all templates now ProcessStartInfo psi; string[] commandargs = sTarget.Split('\t'); if (commandargs.Length == 1) { psi = new ProcessStartInfo(commandargs[0]); } else { string commandExe = commandargs[0]; StringBuilder sb = new StringBuilder(); for (int i = 1; i < commandargs.Length; i++) sb.Append("\"" + commandargs[i].Replace("\"", "\"\"") + "\" "); string sArgString = sb.ToString(); sArgString = sArgString.Substring(0, sArgString.Length - 1); //get rid of trailing space if it's there psi = new ProcessStartInfo(commandExe, sArgString); } if (cmd.bRemainOpenAfterRunning) { psi.WindowStyle = ProcessWindowStyle.Minimized; psi.RedirectStandardOutput = true; psi.UseShellExecute = false; // necessary for stream redirect psi.CreateNoWindow = true; } else { psi.UseShellExecute = true; // ?? so that directories can be opened } Process process; try { process = System.Diagnostics.Process.Start(psi); } catch (Exception e) //(Win32Exception e) { throw new LnzLaunchDataException("Error: " + e.Message); } if (cmd.bRemainOpenAfterRunning) { StreamReader myOutput = process.StandardOutput; process.WaitForExit(9000); //TODO: let user set this number if (!process.HasExited) throw new LnzLaunchDataException("Process timed out."); return myOutput.ReadToEnd().Trim(); //sometimes output has a leading newline } else return null; }
//NOTE: will throw LnzLaunchDataException upon error. public void loadData(List<KeyValuePair<string, string>> constants, List<KeyValuePair<string, string>> commands, List<KeyValuePair<string, string>> icons) { //load constants m_dictConstants = new Dictionary<string, string>(); /*dictConstants["%clip%"] = null; dictConstants["%1%"] = null; dictConstants["%.%"] = Path.GetDirectoryName(Application.ExecutablePath);*/ foreach (KeyValuePair<string, string> pair in constants) { string constantName = pair.Key; string constantValue = pair.Value; if (constantName=="" || !(constantName.StartsWith("%")&&constantName.EndsWith("%"))) throw new LnzLaunchDataException("Error: constants must be in form %a%. Percentage symbol was missing: '"+constantName+"' ."); if (m_dictConstants.ContainsKey(constantName)) throw new LnzLaunchDataException("Error: constant named '"+constantName+"' already defined."); constantValue = doSubstitutions(constantValue, m_dictConstants); //perform substitutions on the value. so can have constants in constants. m_dictConstants[constantName] = constantValue; } //load commands if (commands == null || commands.Count == 0) throw new LnzLaunchDataException("Error: no commands defined in the configuration file. It's not useful if there aren't any shortcuts."); this.m_arrCommands = null; //stops anything trying to use m_arrCommands before it's been formed List<LnzLaunchCommand> listCommands = new List<LnzLaunchCommand>(); bool bRemainOpen; foreach (KeyValuePair<string, string> pair in commands) { bRemainOpen = false; string commandName = pair.Key; string commandValue = pair.Value; commandName = commandName.Replace("%eq%", "="); commandValue = commandValue.Replace("%eq%", "="); if (commandName.EndsWith("*")) {bRemainOpen=true; commandName = commandName.Substring(0, commandName.Length-1);} if (commandName == "" || commandValue == "") throw new LnzLaunchDataException("Error: command name or target cannot be an empty string."); commandValue = doSubstitutions(commandValue, m_dictConstants); commandValue = commandValue.Replace("%.%", Path.GetDirectoryName(Application.ExecutablePath)); //see if any undefined constants string tmp = commandValue; tmp=tmp.Replace("%clip%", "").Replace("%1%","").Replace("%.%",""); if (tmp.Contains("%")) throw new LnzLaunchDataException("Error: unknown constant in the line '"+commandValue+"'."); //replace multiple tabs (note takes place after substitution) commandValue = commandValue.Replace("\t\t", "\t").Replace("\t\t", "\t").Replace("\t\t", "\t").Replace("\t\t", "\t"); //is it already in our list of commands? for (int i = 0; i < listCommands.Count; i++) { if (listCommands[i].sCmdShortcut == commandName) throw new LnzLaunchDataException("Error: cannot define two commands with the same name. there are two named '" + commandName + "'."); } LnzLaunchCommand cmd = new LnzLaunchCommand(commandName, commandValue, bRemainOpen); listCommands.Add(cmd); } listCommands.Sort(); this.m_arrCommands = listCommands.ToArray(); listCommands = null; //catch anything trying to use this //load icons if (icons != null) { foreach (KeyValuePair<string, string> pair in icons) { string commandName = pair.Key; string iconValue = pair.Value; if (iconValue == "") throw new LnzLaunchDataException("Error: custom icon, cannot have empty string for cmd '" + commandName + "'."); commandName = commandName.Replace("%eq%", "="); if (commandName.EndsWith("*")) { commandName = commandName.Substring(0, commandName.Length - 1); } iconValue = doSubstitutions(iconValue, m_dictConstants); iconValue = iconValue.Replace("%.%", Path.GetDirectoryName(Application.ExecutablePath)); if (iconValue.Contains("%")) throw new LnzLaunchDataException("Error: unknown constant in the icon value '" + iconValue + "' ('clip' or args not valid here)."); int index = lookupExactCommandIndex(commandName); if (index == -1) throw new LnzLaunchDataException("Error: trying to set an icon for command '" + commandName + "' but that command isn't defined."); this.m_arrCommands[index].sCustomIcon = iconValue; } } }