using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Microsoft.VisualStudio.Shell.Interop; using Microsoft.VisualStudio.Shell; namespace ETHZurich.OrigoVSIntegration.Dialogs { public partial class OrigoMainDialog : Form { #region -------------------- Members ------------------------------------ private OrigoProject m_origoProject; private XmlRpcCalls m_XrpcCalls; private string m_session; private string m_username; private string m_password; private ListView m_LivFtpFiles; private ListView m_LivLocalFiles; private bool m_bIgnorePlatformChange; #endregion ----------------- End Members -------------------------------- //*********************************************************************** #region -------------------- Initialisation ----------------------------- public OrigoMainDialog(IVsPackage package) { InitializeComponent(); if (package == null) { throw new NullReferenceException("Package given to OrigoMainDialog is null"); } //get the options page object obj; package.GetAutomationObject("Origo.General", out obj); OrigoSettings origoSettings = (OrigoSettings)obj; if (origoSettings == null) { throw new ApplicationException("No Origo.General options page found"); } m_XrpcCalls = new XmlRpcCalls(origoSettings); } //----------------------------------------------------------------------- private void OrigoMainDialog_Load(object sender, EventArgs e) { LblState.Text = ""; m_session = null; m_username = null; m_password = null; m_bIgnorePlatformChange = false; //check if the client exists if (!System.IO.File.Exists(m_XrpcCalls.m_OrigoSettings.ClientPath)) { MessageBox.Show("File \"" + m_XrpcCalls.m_OrigoSettings.ClientPath + "\" does not exist. Please change the path of the XML RPC commandline tool in your IDE settings.", "Origo XML RPC client not found"); Close(); return; } m_LivFtpFiles = (ListView)TapRelease.Controls.Find("LivFtpFiles", false)[0]; if (m_LivFtpFiles == null) { Close(); return; } m_LivLocalFiles = (ListView)TapUpload.Controls.Find("LivLocalFiles", false)[0]; if (m_LivLocalFiles == null) { Close(); return; } InitProjectList(); m_origoProject = new OrigoProject(); CbProjectNames.SelectedIndex = 0; m_LivLocalFiles.Columns.Add("Name"); m_LivFtpFiles.Columns.Add("Name"); ListViewGroup group = m_LivFtpFiles.Groups.Add(Constants.NoPlatform, Constants.NoPlatform); CbPlatforms.Items.Add(group); CbPlatforms.SelectedIndex = 0; this.Shown += new EventHandler(this.ReceiveOrigoData); } //----------------------------------------------------------------------- //event handler for Shown event (which is called after the dialog is shown) private void ReceiveOrigoData (Object sender, EventArgs e) { if (m_XrpcCalls.m_OrigoSettings.OrigoKey.Equals(String.Empty) || m_XrpcCalls.m_OrigoSettings.OrigoKey.Contains(" ")) { System.Windows.Forms.MessageBox.Show("Please enter a valid Origo user key into your IDE properties", "Origo Error"); Close(); return; } try { //get the session id LblState.Text = "Loggin in..."; m_session = m_XrpcCalls.Login(); if (m_session == null) { LblState.Text = "An error ocurred"; return; } //get your username LblState.Text = "Receiving username..."; m_username = m_XrpcCalls.MyUsername(m_session); if (m_username == null) { LblState.Text = "An error ocurred"; return; } //get your password LblState.Text = "Receiving password..."; m_password = m_XrpcCalls.MyPassword(m_session); if (m_password == null) { LblState.Text = "An error ocurred"; return; } RefreshFtpFileList(); FillProjectList(); LblState.Text = ""; } catch (ApplicationException exception) { LblState.Text = "An error ocurred"; MessageBox.Show(exception.Message, "Error"); Close(); } } #endregion ----------------- End Initialisation ------------------------- //*********************************************************************** #region -------------------- Other Methods ------------------------------ private void InitProjectList() { CbProjectNames.Items.Clear(); CbProjectNames.Items.Add(new OrigoProject()); CbProjectNames.SelectedIndex = 0; } //----------------------------------------------------------------------- private void RefreshFtpFileList() { LblState.Text = "Receiving FTP file list..."; if (m_username == null || m_password == null) { MessageBox.Show("Username or password are not set. Most probably due to connection problems.\nPlease check your internet connection and reopen the Origo dialog."); LblState.Text = "An error ocurred"; return; } List fileList; try { fileList = m_XrpcCalls.FtpFileList(m_username, m_password); ListViewItem item; ListViewGroup group = m_LivFtpFiles.Groups[0]; //go through the files in the list view and delete them if they are not in the list from the server for (int i = m_LivFtpFiles.Items.Count - 1; i >= 0; i--) { if (!fileList.Contains(m_LivFtpFiles.Items[i].Text)) { m_LivFtpFiles.Items.RemoveAt(i); } } //go through the list from the server and add them if they aren't already in the list view for (int i = 0; i < fileList.Count; i++) { if (fileList[i] != "") { //check if the file is already in the list, if so do nothing if (!m_LivFtpFiles.Items.ContainsKey(fileList[i])) { item = m_LivFtpFiles.Items.Add(fileList[i], fileList[i], null); item.Group = group; } } } m_LivFtpFiles.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent); LblState.Text = ""; } catch (ApplicationException e) { MessageBox.Show(e.Message, "Error"); LblState.Text = "An error occurred"; } } //----------------------------------------------------------------------- private void FillProjectList() { //get the project list LblState.Text = "Receiving project list..."; if (m_username == null) { MessageBox.Show("Username is not set. Most probably due to connection problems.\nPlease check your internet connection and reopen the Origo dialog."); LblState.Text = "An error ocurred"; return; } List projectList; try { projectList = m_XrpcCalls.ProjectListOfUser(m_session, m_username); } catch (ApplicationException exception) { MessageBox.Show(exception.Message, "Error"); LblState.Text = "An error occurred"; return; } InitProjectList(); for (int i = 0; i < projectList.Count; i++) { if (projectList[i].isOwned) { CbProjectNames.Items.Add(projectList[i]); } } } #endregion ----------------- End Other Methods -------------------------- //*********************************************************************** #region -------------------- Event Handlers ----------------------------- private void BtnOk_Click(object sender, EventArgs e) { Close(); } //----------------------------------------------------------------------- private void BtnCancel_Click(object sender, EventArgs e) { Close(); } //----------------------------------------------------------------------- private void BtnOpenFile_Click(object sender, EventArgs e) { ListViewItem livItem; OpenFileDialog openFileDlg = new OpenFileDialog(); openFileDlg.Multiselect = true; openFileDlg.ShowDialog(); //int groupIndex = LivLocalFiles. //ListViewGroup group = LivLocalFiles.Groups[groupIndex]; //ListViewGroup group = m_LivLocalFiles.Groups.Add(CbPlatforms.SelectedItem.ToString(), CbPlatforms.SelectedItem.ToString()); //add selected text files to the listview for (int i = 0; i < openFileDlg.FileNames.Length; i++) { //check if the file is already in the list if (!m_LivLocalFiles.Items.ContainsKey(openFileDlg.FileNames[i])) { //livItem = group.Items.Add(openFileDlg.FileNames[i], openFileDlg.FileNames[i], null); livItem = m_LivLocalFiles.Items.Add(openFileDlg.FileNames[i], openFileDlg.FileNames[i], null); //livItem.Group = group; livItem.Checked = true; } //if it is in the list, check it again else { ListViewItem[] livItems = m_LivLocalFiles.Items.Find(openFileDlg.FileNames[i], true); if (livItems.Length > 0) { livItems[0].Checked = true; } } } m_LivLocalFiles.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent); } //----------------------------------------------------------------------- private void BtnDelete_Click(object sender, EventArgs e) { if (m_username == null || m_password == null) { MessageBox.Show("Username or password are not set. Most probably due to connection problems.\n" + "Please check your internet connection and reopen the Origo dialog.", "Origo"); LblState.Text = "An error ocurred"; return; } //generate a messagebox which asks to user to confirm the deletion string errorMessage = "Do your really want to delete following files on the FTP server?\n\n"; for (int i = 0; i < m_LivFtpFiles.SelectedItems.Count; i++) { errorMessage = errorMessage + m_LivFtpFiles.SelectedItems[i].Text + "\n"; } DialogResult confirmation = MessageBox.Show(errorMessage, "Origo FTP delete", MessageBoxButtons.YesNo); if (confirmation == DialogResult.No) { return; } try { //delete the files for (int i = 0; i < m_LivFtpFiles.SelectedItems.Count; i++) { m_XrpcCalls.FtpDelete(m_username, m_password, m_LivFtpFiles.SelectedItems[i].Text); } } catch (ApplicationException exception) { MessageBox.Show(exception.Message, "Error"); LblState.Text = "An error occurred"; } RefreshFtpFileList(); m_LivLocalFiles.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent); } //----------------------------------------------------------------------- private void BtnUpload_Click(object sender, EventArgs e) { if (m_username == null || m_password == null) { MessageBox.Show("Username or password are not set. Most probably due to connection problems.\n" + "Please check your internet connection and reopen the Origo dialog.", "Origo"); LblState.Text = "An error ocurred"; return; } else if (m_LivLocalFiles.CheckedItems.Count == 0) { MessageBox.Show("No files are selected.", "Origo FTP upload"); LblState.Text = ""; return; } //generate a messagebox which asks to user to confirm the upload string errorMessage = "Do your really want to upload following files to the FTP server?\n\n"; for (int i = 0; i < m_LivLocalFiles.CheckedItems.Count; i++) { errorMessage = errorMessage + m_LivLocalFiles.CheckedItems[i].Text + "\n"; } DialogResult confirmation = MessageBox.Show(errorMessage, "Origo FTP upload", MessageBoxButtons.YesNo); if (confirmation == DialogResult.No) { return; } try { //upload the files for (int i = 0; i < m_LivLocalFiles.CheckedItems.Count; i++) { LblState.Text = "Uploading " + m_LivLocalFiles.CheckedItems[i].Text; m_XrpcCalls.FtpUpload(m_username, m_password, m_LivLocalFiles.CheckedItems[i].Text); } } catch (ApplicationException exception) { MessageBox.Show(exception.Message, "Error"); LblState.Text = "An error occurred"; } m_LivLocalFiles.Items.Clear(); m_LivLocalFiles.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent); RefreshFtpFileList(); } //----------------------------------------------------------------------- private void BtnAddPlatform_Click(object sender, EventArgs e) { PlatformDialog dlg = new PlatformDialog(); dlg.ShowDialog(this); if (dlg.Result == DialogResult.Cancel) { return; } ListViewGroup group = m_LivFtpFiles.Groups.Add(dlg.Platform, dlg.Platform); CbPlatforms.Items.Add(group); m_LivFtpFiles.Select(); } //----------------------------------------------------------------------- private void BtnRelease_Click(object sender, EventArgs e) { if (CbProjectNames.Text.Equals(Constants.projGlobNoProj)) { MessageBox.Show("You have to select an Origo project.", "Error"); return; } ReleaseDialog dlg = new ReleaseDialog(); dlg.ShowDialog(this); if (dlg.Result != DialogResult.OK) { return; } string message = "Do you really want to build this release?\n\n"; message += "Origo project name: " + CbProjectNames.Text + "\n"; message += "Release name: " + dlg.ReleaseName + "\n"; message += "Version: " + dlg.ReleaseVersion + "\n"; message += "Description:\n"; message += dlg.ReleaseDescription + "\n\n"; ListViewGroup group; //m_LifFtpFiles.Groups[0] are the files with no specified platform and hence which are not released for (int i = 1; i < m_LivFtpFiles.Groups.Count; i++) { group = m_LivFtpFiles.Groups[i]; //ignore the group if it has no items if (group.Items.Count == 0) { continue; } message += group.Name + " files:\n"; //add the files for (int j = 0; j < group.Items.Count; j++) { message += "\t" + group.Items[j].Text + "\n"; } message += "\n"; } //also list the files that are not released group = m_LivFtpFiles.Groups[0]; if (group.Items.Count > 0) { message += "Files which are not used for this release:\n"; for (int j = 0; j < group.Items.Count; j++) { message += "\t" + group.Items[j].Text + "\n"; } } //show a confirmation dialog if (MessageBox.Show(message, "Origo Release", MessageBoxButtons.OKCancel) != DialogResult.OK) { return; } try { m_XrpcCalls.Release(m_session, m_LivFtpFiles, dlg, ((OrigoProject)CbProjectNames.SelectedItem).id); } catch (ApplicationException exception) { MessageBox.Show(exception.Message, "Error"); LblState.Text = "An error occurred"; } RefreshFtpFileList(); } //----------------------------------------------------------------------- //select the group of the selected item in the platform list private void LivFtpFiles_SelectedIndexChanged(object sender, EventArgs e) { //check if anything is selected if (m_LivFtpFiles.SelectedItems.Count == 0) { CbPlatforms.Enabled = false; return; } CbPlatforms.Enabled = true; ListViewItem item = m_LivFtpFiles.SelectedItems[0]; m_bIgnorePlatformChange = true; CbPlatforms.SelectedIndex = CbPlatforms.Items.IndexOf(item.Group); m_bIgnorePlatformChange = false; } //----------------------------------------------------------------------- //set the group of all selected items in the ftp file list to the newly selected one private void CbPlatforms_SelectedIndexChanged(object sender, EventArgs e) { //if the selection change is caused by a selection change on m_LivFtpFiles do nothing if (m_bIgnorePlatformChange) { return; } ListViewGroup group = (ListViewGroup) CbPlatforms.SelectedItem; if (group == null) { MessageBox.Show("An error occurred", "Origo"); return; } for (int i = 0; i < m_LivFtpFiles.SelectedItems.Count; i++) { m_LivFtpFiles.SelectedItems[i].Group = group; } m_LivFtpFiles.Select(); } //----------------------------------------------------------------------- #endregion ----------------- End Event Handlers ------------------------- //*********************************************************************** } }