private static List <T> GetAllRecords <T>(Net.Api.RedmineManager manager, NameValueCollection parameters = null) where T : class, new() { if (parameters == null) { parameters = new NameValueCollection(); } IList <T> redmineAllRecordsResult; List <T> allRecords = new List <T>(); int limit = 100; int offset = 0; do { parameters["offset"] = offset.ToString(); parameters["limit"] = limit.ToString(); redmineAllRecordsResult = manager.GetObjectList <T>(parameters); allRecords.AddRange(redmineAllRecordsResult); offset += redmineAllRecordsResult.Count; }while (redmineAllRecordsResult.Count == limit); return(allRecords); }
private List <ContactViewModel> LoadContactsFromRedmine() { var contactsList = new List <ContactViewModel>(); IList <Net.Api.Types.Contact> contacts = null; try { // connect to redmine Net.Api.RedmineManager manager = new Net.Api.RedmineManager(Settings.Default.RedmineServer, Settings.Default.RedmineApi, Net.Api.MimeFormat.xml); var contact = manager.GetObject <Net.Api.Types.Contact>("1477", new System.Collections.Specialized.NameValueCollection { }); contacts = manager.GetAllObjectList <Net.Api.Types.Contact>(new System.Collections.Specialized.NameValueCollection { }); } catch { } if (contacts == null) { return(null); } foreach (var contact in contacts) { ContactViewModel contactViewModel = new ContactViewModel(); contactViewModel.Id = contact.Id; contactViewModel.FirstName = contact.FirstName; contactViewModel.LastName = contact.LastName; if (contact.ContactType != null) { ContactTypeViewModel contactType = new ContactTypeViewModel(); contactType.Id = contact.ContactType.Id; contactType.Name = contact.ContactType.Name; contactViewModel.ContactType = contactType; } //if (contact.CustomFields != null) //{ // foreach (var customField in contact.CustomFields) // { // CustomFieldViewModel customFieldViewModel = new CustomFieldViewModel(); // customFieldViewModel.Id = customField.Id; // customFieldViewModel.Name = customField.Name; // contactViewModel.CustomFields.Add(customFieldViewModel); // } //} contactsList.Add(contactViewModel); } return(contactsList); }
private Net.Api.RedmineManager GetRedmineManager() { if (string.IsNullOrEmpty(Settings.Default.RedmineServer)) { return(null); } Net.Api.RedmineManager manager = new Net.Api.RedmineManager(Settings.Default.RedmineServer, Settings.Default.RedmineApi, Net.Api.MimeFormat.xml); return(manager); }
private void buttonClose_Click(object sender, RoutedEventArgs e) { try { // connect to redmine Net.Api.RedmineManager manager = new Net.Api.RedmineManager(Settings.Default.RedmineServer, Settings.Default.RedmineApi, Net.Api.MimeFormat.xml); var user = manager.GetCurrentUser(); if (user.Id > 0) { Settings.Default.Save(); DialogResult = true; } } catch { MessageBox.Show("Cannot connect to the Redmine. Please check your configuration", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void DoUpdateRedmineUser() { try { if (string.IsNullOrEmpty(Settings.Default.RedmineServer) == false && string.IsNullOrEmpty(Settings.Default.RedmineApi) == false) { Net.Api.RedmineManager manager = GetRedmineManager(); var user = manager.GetCurrentUser(); _userName = string.Format("{0} {1}", user.FirstName, user.LastName); _currentRedmineUser = user; _selectProjectViewModel.ReloadProjectsList(); // now check if we have EasyRedmine extended API try { var contacts = manager.GetObjectList <Net.Api.Types.Contact>(new NameValueCollection() { }); _isEasyContactApiAvailable = true; } catch (RedmineException e) { _isEasyContactApiAvailable = false; } InvalidateRibbon(); } } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Error: {0}", e.Message); _userName = string.Empty; InvalidateRibbon(); } }
private void buttonClose_Click(object sender, RoutedEventArgs e) { try { // connect to redmine Net.Api.RedmineManager manager = new Net.Api.RedmineManager(serverTextBox.Text, apiKeyTextBox.Text, Net.Api.MimeFormat.xml); var user = manager.GetCurrentUser(); if (user.Id > 0) { Settings.Default.RedmineApi = apiKeyTextBox.Text; Settings.Default.RedmineServer = serverTextBox.Text; Settings.Default.OpenTaskWhenCreated = openTaskInBrowser.IsChecked.HasValue && openTaskInBrowser.IsChecked.Value == true; Settings.Default.Save(); DialogResult = true; } } catch { MessageBox.Show("Cannot connect to the Redmine. Please check your configuration", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
private List <ProjectViewModel> LoadProjectsFromRedmine() { var projectsList = new List <ProjectViewModel>(); IList <Net.Api.Types.Project> projects = null; try { // connect to redmine Net.Api.RedmineManager manager = new Net.Api.RedmineManager(Settings.Default.RedmineServer, Settings.Default.RedmineApi, Net.Api.MimeFormat.xml); var parameters = new NameValueCollection { { "limit", "100" }, { "include", "trackers,issue_categories" } }; projects = GetAllRecords <Net.Api.Types.Project>(manager, parameters); } catch { } if (projects == null) { return(null); } foreach (var project in projects) { ProjectViewModel projectViewModel = new ProjectViewModel { Id = project.Id, Name = project.Name }; if (project.Parent != null) { projectViewModel.ParentId = project.Parent.Id; } if (project.CustomFields != null) { foreach (var customField in project.CustomFields) { CustomFieldViewModel customFieldViewModel = new CustomFieldViewModel { Id = customField.Id, Name = customField.Name }; projectViewModel.CustomFields.Add(customFieldViewModel); } } if (project.Trackers != null) { foreach (var tracker in project.Trackers) { TrackerViewModel trackerViewModel = new TrackerViewModel { Id = tracker.Id, Name = tracker.Name }; projectViewModel.Trackers.Add(trackerViewModel); // set first one as default if (projectViewModel.Tracker == null) { projectViewModel.Tracker = trackerViewModel; } } } if (project.IssueCategories != null) { foreach (var issueCategory in project.IssueCategories) { IssueCategoryViewModel issueCategoryViewModel = new IssueCategoryViewModel { Id = issueCategory.Id, Name = issueCategory.Name }; projectViewModel.IssueCategories.Add(issueCategoryViewModel); } } projectsList.Add(projectViewModel); } return(projectsList); }
private List<ContactViewModel> LoadContactsFromRedmine() { var contactsList = new List<ContactViewModel>(); IList<Net.Api.Types.Contact> contacts = null; try { // connect to redmine Net.Api.RedmineManager manager = new Net.Api.RedmineManager(Settings.Default.RedmineServer, Settings.Default.RedmineApi, Net.Api.MimeFormat.xml); contacts = manager.GetAllObjectList<Net.Api.Types.Contact>(new System.Collections.Specialized.NameValueCollection { }); } catch { } if (contacts == null) { return null; } foreach (var contact in contacts) { ContactViewModel contactViewModel = new ContactViewModel(); contactViewModel.Id = contact.Id; contactViewModel.FirstName = contact.FirstName; contactViewModel.LastName = contact.LastName; if (contact.ContactType != null) { ContactTypeViewModel contactType = new ContactTypeViewModel(); contactType.Id = contact.ContactType.Id; contactType.Name = contact.ContactType.Name; contactViewModel.ContactType = contactType; } //if (contact.CustomFields != null) //{ // foreach (var customField in contact.CustomFields) // { // CustomFieldViewModel customFieldViewModel = new CustomFieldViewModel(); // customFieldViewModel.Id = customField.Id; // customFieldViewModel.Name = customField.Name; // contactViewModel.CustomFields.Add(customFieldViewModel); // } //} contactsList.Add(contactViewModel); } return contactsList; }
/// <summary> /// Fired when user clicks on convert button /// </summary> public void OnConvertEmailButtonClick(Office.IRibbonControl control) { bool isOwnerSet = false; Outlook.MailItem mail = GetSelectedMail(control); if (mail == null) { return; } // if no settings is saved prompt user to fill it if (string.IsNullOrEmpty(_userName)) { DoShowSettings(); // if still no password, skip it... if (string.IsNullOrEmpty(_userName)) { return; } } // Ask for project SelectProjectWindow projectWindow = new SelectProjectWindow(); projectWindow.DataContext = _selectProjectViewModel; _selectProjectViewModel.SetSelectedProject(Settings.Default.LastUsedProjectId); // use WindowInteropHelper to set the Owner of our WPF window to the Outlook application window System.Windows.Interop.WindowInteropHelper hwndHelper = new System.Windows.Interop.WindowInteropHelper(projectWindow); hwndHelper.Owner = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle; // new IntPtr(Globals.ThisAddIn.Application.ActiveWindow().WindowHandle32); // show our window bool?result = projectWindow.ShowDialog(); if ((result.HasValue && result.Value == false) || _selectProjectViewModel.SelectedProject == null) { // Cancel task return; } // Save last used project id Settings.Default.LastUsedProjectId = _selectProjectViewModel.SelectedProject.Id; Settings.Default.Save(); Net.Api.RedmineManager manager = new Net.Api.RedmineManager(Settings.Default.RedmineServer, Settings.Default.RedmineApi, Net.Api.MimeFormat.xml); Net.Api.Types.Issue issue = new Net.Api.Types.Issue(); issue.Subject = mail.Subject; if (mail.BodyFormat == Outlook.OlBodyFormat.olFormatHTML) { issue.Description = mail.HTMLBody; } else { issue.Description = mail.Body; } issue.Project = new Net.Api.Types.Project() { Id = _selectProjectViewModel.SelectedProject.Id }; if (_selectProjectViewModel.SelectedProject.Tracker != null) { issue.Tracker = new Net.Api.Types.IdentifiableName() { Id = _selectProjectViewModel.SelectedProject.Tracker.Id }; } if (_selectProjectViewModel.SelectedProject.IssueCategory != null) { issue.Category = new Net.Api.Types.IdentifiableName() { Id = _selectProjectViewModel.SelectedProject.IssueCategory.Id }; } try { var users = manager.GetObjectList <Net.Api.Types.User>(new NameValueCollection { { "name", GetSenderSMTPAddress(mail) } }); if (users.Count == 1) { issue.Author = new Net.Api.Types.IdentifiableName() { Id = users.FirstOrDefault().Id }; isOwnerSet = true; } } catch { } issue.AssignedTo = new Net.Api.Types.IdentifiableName() { Id = _currentRedmineUser.Id }; List <Net.Api.Types.Upload> attachments = new List <Net.Api.Types.Upload>(); if (mail.Attachments.Count > 0) { foreach (Outlook.Attachment att in mail.Attachments) { try { string tempFile = Path.GetTempFileName(); att.SaveAsFile(tempFile); var upload = manager.UploadFile(File.ReadAllBytes(tempFile)); upload.FileName = att.FileName; upload.Description = att.DisplayName; upload.ContentType = System.Web.MimeMapping.GetMimeMapping(att.FileName); attachments.Add(upload); } catch (Exception e) { MessageBox.Show(string.Format("Cannot upload attachment {0}. Error: {1}", att.FileName, e.Message)); } } } Net.Api.Types.Issue createdIssue = null; try { issue.Uploads = attachments; createdIssue = manager.CreateObject(issue); } catch { MessageBox.Show("Creation of the task failed."); return; } // also if setting the owner failed (no admin rights) try to note owner at least to the custom field if is assigned to the project if (isOwnerSet == false) { try { var list = new List <Net.Api.Types.IssueCustomField>(); var value = new Net.Api.Types.CustomFieldValue() { Info = GetSenderSMTPAddress(mail) }; var values = new List <Net.Api.Types.CustomFieldValue>(); values.Add(value); var field = new Net.Api.Types.IssueCustomField() { Id = Settings.Default.OwnerEmailCustomFieldId, Values = values }; //owner-email list.Add(field); createdIssue.Notes = "Ownership of the task changed to the original sender of the e-mail."; createdIssue.CustomFields = list; manager.UpdateObject(createdIssue.Id.ToString(), createdIssue); } catch { } } if (Settings.Default.OpenTaskWhenCreated) { System.Diagnostics.Process.Start(string.Format("{0}/issues/{1}", Settings.Default.RedmineServer, createdIssue.Id)); } else { MessageBox.Show(string.Format("Task has been created in Redmine with ID #{0}.", createdIssue.Id)); } }
private List<ProjectViewModel> LoadProjectsFromRedmine() { var projectsList = new List<ProjectViewModel>(); IList<Net.Api.Types.Project> projects = null; try { // connect to redmine Net.Api.RedmineManager manager = new Net.Api.RedmineManager(Settings.Default.RedmineServer, Settings.Default.RedmineApi, Net.Api.MimeFormat.xml); projects = manager.GetObjectList<Net.Api.Types.Project>(new System.Collections.Specialized.NameValueCollection { { "limit", "100" }, { "include", "trackers,issue_categories" } }); } catch { } if (projects == null) { return null; } foreach (var project in projects) { ProjectViewModel projectViewModel = new ProjectViewModel(); projectViewModel.Id = project.Id; projectViewModel.Name = project.Name; if (project.Parent != null) { projectViewModel.ParentId = project.Parent.Id; } if (project.CustomFields != null) { foreach (var customField in project.CustomFields) { CustomFieldViewModel customFieldViewModel = new CustomFieldViewModel(); customFieldViewModel.Id = customField.Id; customFieldViewModel.Name = customField.Name; projectViewModel.CustomFields.Add(customFieldViewModel); } } if (project.Trackers != null) { foreach (var tracker in project.Trackers) { TrackerViewModel trackerViewModel = new TrackerViewModel(); trackerViewModel.Id = tracker.Id; trackerViewModel.Name = tracker.Name; projectViewModel.Trackers.Add(trackerViewModel); // set first one as default if (projectViewModel.Tracker == null) { projectViewModel.Tracker = trackerViewModel; } } } if (project.IssueCategories != null) { foreach (var issueCategory in project.IssueCategories) { IssueCategoryViewModel issueCategoryViewModel = new IssueCategoryViewModel(); issueCategoryViewModel.Id = issueCategory.Id; issueCategoryViewModel.Name = issueCategory.Name; projectViewModel.IssueCategories.Add(issueCategoryViewModel); } } projectsList.Add(projectViewModel); } return projectsList; }
private Net.Api.RedmineManager GetRedmineManager() { if (string.IsNullOrEmpty(Settings.Default.RedmineServer)) return null; Net.Api.RedmineManager manager = new Net.Api.RedmineManager(Settings.Default.RedmineServer, Settings.Default.RedmineApi, Net.Api.MimeFormat.xml); return manager; }
/// <summary> /// Fired when user clicks on convert button /// </summary> public void OnConvertEmailButtonClick(Office.IRibbonControl control) { bool isOwnerSet = false; Outlook.MailItem mail = GetSelectedMail(control); if (mail == null) { return; } // if no settings is saved prompt user to fill it if (string.IsNullOrEmpty(_userName)) { DoShowSettings(); // if still no password, skip it... if (string.IsNullOrEmpty(_userName)) { return; } } // Ask for project SelectProjectWindow projectWindow = new SelectProjectWindow(); projectWindow.DataContext = _selectProjectViewModel; _selectProjectViewModel.SetSelectedProject(Settings.Default.LastUsedProjectId); // use WindowInteropHelper to set the Owner of our WPF window to the Outlook application window System.Windows.Interop.WindowInteropHelper hwndHelper = new System.Windows.Interop.WindowInteropHelper(projectWindow); hwndHelper.Owner = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle; // new IntPtr(Globals.ThisAddIn.Application.ActiveWindow().WindowHandle32); // show our window bool? result = projectWindow.ShowDialog(); if ((result.HasValue && result.Value == false) || _selectProjectViewModel.SelectedProject == null) { // Cancel task return; } // Save last used project id Settings.Default.LastUsedProjectId = _selectProjectViewModel.SelectedProject.Id; Settings.Default.Save(); Net.Api.RedmineManager manager = new Net.Api.RedmineManager(Settings.Default.RedmineServer, Settings.Default.RedmineApi, Net.Api.MimeFormat.xml); Net.Api.Types.Issue issue = new Net.Api.Types.Issue(); issue.Subject = mail.Subject; if (mail.BodyFormat == Outlook.OlBodyFormat.olFormatHTML) { issue.Description = mail.HTMLBody; } else { issue.Description = mail.Body; } issue.Project = new Net.Api.Types.Project() { Id = _selectProjectViewModel.SelectedProject.Id }; if (_selectProjectViewModel.SelectedProject.Tracker != null) { issue.Tracker = new Net.Api.Types.IdentifiableName() { Id = _selectProjectViewModel.SelectedProject.Tracker.Id }; } if (_selectProjectViewModel.SelectedProject.IssueCategory != null) { issue.Category = new Net.Api.Types.IdentifiableName() { Id = _selectProjectViewModel.SelectedProject.IssueCategory.Id }; } try { var users = manager.GetObjectList<Net.Api.Types.User>(new NameValueCollection { { "name", GetSenderSMTPAddress(mail) } }); if (users.Count == 1) { issue.Author = new Net.Api.Types.IdentifiableName() { Id = users.FirstOrDefault().Id }; isOwnerSet = true; } } catch { } issue.AssignedTo = new Net.Api.Types.IdentifiableName() { Id = _currentRedmineUser.Id }; List<Net.Api.Types.Upload> attachments = new List<Net.Api.Types.Upload>(); if (mail.Attachments.Count > 0) { foreach (Outlook.Attachment att in mail.Attachments) { try { string tempFile = Path.GetTempFileName(); att.SaveAsFile(tempFile); var upload = manager.UploadFile(File.ReadAllBytes(tempFile)); upload.FileName = att.FileName; upload.Description = att.DisplayName; upload.ContentType = System.Web.MimeMapping.GetMimeMapping(att.FileName); attachments.Add(upload); } catch (Exception e) { MessageBox.Show(string.Format("Cannot upload attachment {0}. Error: {1}", att.FileName, e.Message)); } } } Net.Api.Types.Issue createdIssue = null; try { issue.Uploads = attachments; createdIssue = manager.CreateObject(issue); } catch { MessageBox.Show("Creation of the task failed."); return; } // also if setting the owner failed (no admin rights) try to note owner at least to the custom field if is assigned to the project if (isOwnerSet == false) { try { var list = new List<Net.Api.Types.IssueCustomField>(); var value = new Net.Api.Types.CustomFieldValue() { Info = GetSenderSMTPAddress(mail) }; var values = new List<Net.Api.Types.CustomFieldValue>(); values.Add(value); var field = new Net.Api.Types.IssueCustomField() { Id = Settings.Default.OwnerEmailCustomFieldId, Values = values }; //owner-email list.Add(field); createdIssue.Notes = "Ownership of the task changed to the original sender of the e-mail."; createdIssue.CustomFields = list; manager.UpdateObject(createdIssue.Id.ToString(), createdIssue); } catch { } } if (Settings.Default.OpenTaskWhenCreated) { System.Diagnostics.Process.Start(string.Format("{0}/issues/{1}", Settings.Default.RedmineServer, createdIssue.Id)); } else { MessageBox.Show(string.Format("Task has been created in Redmine with ID #{0}.", createdIssue.Id)); } }