Introduction
SweetCuts utility allows user to create links to folders, Webpage and launch file. It also allows to group them in to categories. So you can have a category called Home, Office, General etc. and each category in turn could have many links to folders/Webpage/file. Each category is a tabpage and in each tab page the links are represented as small buttons. User can click the button to initiate action.
Using the Utility
System Requirements to Run the Utility
.NET 2.0 or higher
Architecture
The application reads from config.xml and creates tabs for each category and buttons for each link/folder/file. All the updation to the XML is done using XPATH queries.
The below code reads from config.xml file and updates the list of Params structure
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(m_sConfigFilePath);
XmlElement xmlRoot = xmlDoc.DocumentElement;
XmlNodeList xmlNodeList = xmlRoot.GetElementsByTagName(StringConsts.sCategories);
foreach (XmlNode xmlNode in xmlNodeList)
{
if (xmlNode.HasChildNodes)
{
foreach (XmlNode xmlCategoryNode in xmlNode)
{
if (xmlCategoryNode.HasChildNodes)
{
foreach (XmlNode xmlChild in xmlCategoryNode)
{
XmlAttributeCollection xmlCol = xmlChild.Attributes;
Params oParams = new Params();
foreach (XmlAttribute xmlAttrib in xmlCol)
{
if (xmlAttrib.Name == StringConsts.sDisplayNameAttr)
{
oParams.SetDisplayName(xmlAttrib.Value);
}
else if (xmlAttrib.Name == StringConsts.sPathAttr)
{
oParams.SetLinkPath(xmlAttrib.Value);
}
else if (xmlAttrib.Name == StringConsts.sLaunchTypeAttr)
{
oParams.SetLaunchType(xmlAttrib.Value);
}
}
oParams.SetCategory(xmlCategoryNode.Name);
m_arObjParams.Add(oParams);
}
}
}
}
}
...
And once the content of the XML is updated to the list of Params, we create the tabpages with the buttons on it.
The below code does this
private bool CreateControls()
{
try
{
List<Params> ConfigParams = m_Config.GetConfig();
foreach (Params oParams in ConfigParams)
{
TabPage oPage = GetTabPage(oParams);
Point oPoint = GetNextBtnLocation(oPage);
Button oBtn = CreateBtn(oPoint, oParams);
oPage.Controls.Add(oBtn);
}
return true;
}
catch (Exception e)
{
MessageBoxEx.Show(this,"Error in CreateControls - Description " + e.ToString());
return false;
}
}
...
Adding new Items
After launching the SweetCuts executable, user can add links to folder, webpage or select an file by a click to Add button or by pressing Alt+A key.
- User will have to select from existing category or add new category from category combo box.
- The type of launch. Currently it could be WebBrowser(for URL)/WindowsExplorer (for folders)/Executable(any exe or other types of file which has association to executable say a txt file).
- Enter the Name which will be displayed on the link button. This has to be unique. It serves no purpose to have two button with the same text on it.
- Enter the path of the url or select the folder or file from the folder/file selection dialog.
- Click Add or press Alt+A key to add the item.
- Once done adding items click cancel or press Escape key.
Once all the validation of the input is done, the config.xml is updated with the contents and then the UI is refreshed to make the new changes visible.
The below code shows how the config.xml is updated using xpath query. All the XML parsing and updation code is stashed in to Config class. The client (here the dialog classes) creates object of Config
class and calls methods on it.
public bool AddItem(Params Details)
{
if (!IsValidItem(Details.GetCategory(), Details.GetDisplayName()))
{
return false;
}
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(m_sConfigFilePath);
string xmlPath = string.Format("//{0}/{1}", StringConsts.sCategories, Details.GetCategory());
XmlNode node = xmlDoc.SelectSingleNode(xmlPath);
if (node == null)
{
XmlElement xNewCategory = xmlDoc.CreateElement(Details.GetCategory());
string path = string.Format("//{0}", StringConsts.sCategories);
XmlNode parent = xmlDoc.SelectSingleNode(path);
node = parent.AppendChild(xNewCategory);
}
XmlElement xNewChild = xmlDoc.CreateElement(StringConsts.sItem);
xNewChild.SetAttribute(StringConsts.sDisplayNameAttr, Details.GetDisplayName());
xNewChild.SetAttribute(StringConsts.sPathAttr, Details.GetLinkPath());
xNewChild.SetAttribute(StringConsts.sLaunchTypeAttr, Details.GetLaunchType());
node.AppendChild(xNewChild);
SaveConfig(ref xmlDoc);
return true;
}
}
...
Editing Existing Item
User can edit the item by Right click the link button and select the Edit option.
- Edit item will bring a page with existing detail. User can update the diaog and press update button or Alt+U to save changes. Once done user can press Escape key to come out of the dialog.
- User can delete the link by selecting Delete from the context menu.
How we update the config xml? We actually delete the element and add a new one to the xml. The below code is for deleting an item from the xml. Add item code was listed above.
public bool DeleteItem(Params Details)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(m_sConfigFilePath);
XmlElement xmlRoot = xmlDoc.DocumentElement;
string xmlPath = string.Format("//{0}/{1}[@{2}='{3}']",Details.GetCategory(), StringConsts.sItem, StringConsts.sDisplayNameAttr, Details.GetDisplayName());
XmlNode node = xmlRoot.SelectSingleNode(xmlPath);
if (node == null)
{
return false;
}
node.ParentNode.RemoveChild(node);
SaveConfig(ref xmlDoc);
return true;
}
...
Deleting an Existing Item
User can delete an item by Right click the link button and select the Delete option.
Usability
Minimize to System Tray
User can do away with the dialog once done using it by either hitting the close button or pressing the Esc key. This would keep the program alive but minimize it to the system tray with an visible pin icon.
private void OnKeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == Convert.ToChar(Keys.Escape))
{
this.Hide();
NotifyIcon.Visible = true;
}
}
...
User can view the path of the link (URL for webbrowser link/ Folder path for the windowsexplorer link or file path for executable link) on the tool tip which is visible when the mouse is hovered on the link button
Restore from System Tray
User can click the tray icon to restore the dialog and continue playing with it or user can press Shift + Q to bring the dialog to the front. I have used Googled code to hook key events and handle Shift+Q. I have also overloaded Show to restore the dialog from minimized position and bring the dialog to the front. The below code does exactly that.
new public void Show()
{
this.TopMost = true;
this.Focus();
this.BringToFront();
this.TopMost = false;
this.WindowState = FormWindowState.Normal;
base.Show();
NotifyIcon.Visible = false;
}
...
Exit the Utility
User can exit the utility by doing a click on the close button or by invoking the context menu on the tray icon and selecting exit from it.
Points of Interest
My expertise is VC++. This is my first utility using C#, so everything was a learning for me.
Have been working on C++ for the past 7 years.
An amateur in C#. But given an idea can google and implement anything. Like to read and find new ways of doing things.