diff --git a/src/context.cc b/src/context.cc index f985748fd0..c9b3191923 100644 --- a/src/context.cc +++ b/src/context.cc @@ -932,7 +932,7 @@ void Context::updateUI(const UIElements &what) { if (what.display_time_entry_autocomplete) { if (what.first_load) { if (user_) { - user_->related.TimeEntryAutocompleteItems(&time_entry_autocompletes); + user_->related.TimeEntryAutocompleteItems(&time_entry_autocompletes, user_->DefaultWID()); } UI()->DisplayTimeEntryAutocomplete(&time_entry_autocompletes); } else { @@ -945,7 +945,7 @@ void Context::updateUI(const UIElements &what) { if (what.display_mini_timer_autocomplete) { if (what.first_load) { if (user_) { - user_->related.MinitimerAutocompleteItems(&minitimer_autocompletes); + user_->related.MinitimerAutocompleteItems(&minitimer_autocompletes, user_->DefaultWID()); } UI()->DisplayMinitimerAutocomplete(&minitimer_autocompletes); } else { @@ -1000,7 +1000,7 @@ void Context::updateUI(const UIElements &what) { if (what.display_project_autocomplete) { if (what.first_load) { if (user_) { - user_->related.ProjectAutocompleteItems(&project_autocompletes); + user_->related.ProjectAutocompleteItems(&project_autocompletes, user_->DefaultWID()); } UI()->DisplayProjectAutocomplete(&project_autocompletes); } else { @@ -1119,7 +1119,7 @@ void Context::Sync() { void Context::onTimeEntryAutocompletes(Poco::Util::TimerTask&) { // NOLINT std::vector time_entry_autocompletes; if (user_) { - user_->related.TimeEntryAutocompleteItems(&time_entry_autocompletes); + user_->related.TimeEntryAutocompleteItems(&time_entry_autocompletes, user_->DefaultWID()); } UI()->DisplayTimeEntryAutocomplete(&time_entry_autocompletes); } @@ -1127,7 +1127,7 @@ void Context::onTimeEntryAutocompletes(Poco::Util::TimerTask&) { // NOLINT void Context::onMiniTimerAutocompletes(Poco::Util::TimerTask&) { // NOLINT std::vector minitimer_autocompletes; if (user_) { - user_->related.MinitimerAutocompleteItems(&minitimer_autocompletes); + user_->related.MinitimerAutocompleteItems(&minitimer_autocompletes, user_->DefaultWID()); } UI()->DisplayMinitimerAutocomplete(&minitimer_autocompletes); } @@ -1135,7 +1135,7 @@ void Context::onMiniTimerAutocompletes(Poco::Util::TimerTask&) { // NOLINT void Context::onProjectAutocompletes(Poco::Util::TimerTask&) { // NOLINT std::vector project_autocompletes; if (user_) { - user_->related.ProjectAutocompleteItems(&project_autocompletes); + user_->related.ProjectAutocompleteItems(&project_autocompletes, user_->DefaultWID()); } UI()->DisplayProjectAutocomplete(&project_autocompletes); } diff --git a/src/related_data.cc b/src/related_data.cc index 51561d916a..5ee043789f 100644 --- a/src/related_data.cc +++ b/src/related_data.cc @@ -395,96 +395,116 @@ void RelatedData::taskAutocompleteItems( } } -// Add projects, in format: -// Project. Client -void RelatedData::projectAutocompleteItems( - std::set *unique_names, - std::map *ws_names, - std::vector *list, - std::map > *items, - std::map > *task_items) const { +void RelatedData::projectAutocompleteItem( + std::set* unique_names, + std::map* ws_names, + std::vector* list, + std::map >* items, + std::map >* task_items, + Project *p) const +{ + if (!p->Active()) { + return; + } - poco_check_ptr(list); + Client* c = clientByProject(p); - for (std::vector::const_iterator it = - Projects.begin(); - it != Projects.end(); ++it) { - Project *p = *it; + std::string text = Formatter::JoinTaskName(nullptr, p); + if (text.empty()) { + return; + } - if (!p->Active()) { - continue; + { + std::stringstream ss; + ss << p->WID() << "/" << text; + std::string uniq_name = ss.str(); + if (unique_names->find(uniq_name) != unique_names->end()) { + return; } + unique_names->insert(uniq_name); + } - Client *c = clientByProject(p); - - std::string text = Formatter::JoinTaskName(nullptr, p); - if (text.empty()) { - continue; + view::Autocomplete autocomplete_item; + autocomplete_item.Text = text; + autocomplete_item.ProjectAndTaskLabel = text; + autocomplete_item.ProjectLabel = p->Name(); + if (c) { + autocomplete_item.ClientLabel = c->Name(); + autocomplete_item.ClientID = c->ID(); + } + autocomplete_item.ProjectID = p->ID(); + autocomplete_item.ProjectGUID = p->GUID(); + autocomplete_item.ProjectColor = p->ColorCode(); + if (ws_names) { + autocomplete_item.WorkspaceName = (*ws_names)[p->WID()]; + } + autocomplete_item.WorkspaceID = p->WID(); + autocomplete_item.Type = kAutocompleteItemProject; + + if (items && !autocomplete_item.WorkspaceName.empty()) { + (*items)[autocomplete_item.WorkspaceName].push_back(autocomplete_item); + if (task_items) { + for (std::vector::const_iterator it = + (*task_items)[autocomplete_item.ProjectID].begin(); + it != (*task_items)[autocomplete_item.ProjectID].end(); ++it) { + view::Autocomplete ac = *it; + (*items)[autocomplete_item.WorkspaceName].push_back(ac); + } } + } + else { + list->push_back(autocomplete_item); - { - std::stringstream ss; - ss << p->WID() << "/" << text; - std::string uniq_name = ss.str(); - if (unique_names->find(uniq_name) != unique_names->end()) { - continue; + if (task_items) { + for (std::vector::const_iterator it = + (*task_items)[autocomplete_item.ProjectID].begin(); + it != (*task_items)[autocomplete_item.ProjectID].end(); ++it) { + view::Autocomplete ac = *it; + list->push_back(ac); } - unique_names->insert(uniq_name); } + } +} - view::Autocomplete autocomplete_item; - autocomplete_item.Text = text; - autocomplete_item.ProjectAndTaskLabel = text; - autocomplete_item.ProjectLabel = p->Name(); - if (c) { - autocomplete_item.ClientLabel = c->Name(); - autocomplete_item.ClientID = c->ID(); - } - autocomplete_item.ProjectID = p->ID(); - autocomplete_item.ProjectGUID = p->GUID(); - autocomplete_item.ProjectColor = p->ColorCode(); - if (ws_names) { - autocomplete_item.WorkspaceName = (*ws_names)[p->WID()]; +// Add projects, in format: +// Project. Client +void RelatedData::projectAutocompleteItems( + std::set *unique_names, + std::map *ws_names, + std::vector *list, + std::map > *items, + std::map > *task_items, + const Poco::UInt64 defaultWID) const { + + poco_check_ptr(list); + + for (auto p : Projects) { + if (p->WID() == defaultWID) { + projectAutocompleteItem(unique_names, ws_names, list, items, task_items, p); } - autocomplete_item.WorkspaceID = p->WID(); - autocomplete_item.Type = kAutocompleteItemProject; + } - if (items && !autocomplete_item.WorkspaceName.empty()) { - (*items)[autocomplete_item.WorkspaceName].push_back(autocomplete_item); - if (task_items) { - for (std::vector::const_iterator it = - (*task_items)[autocomplete_item.ProjectID].begin(); - it != (*task_items)[autocomplete_item.ProjectID].end(); ++it) { - view::Autocomplete ac = *it; - (*items)[autocomplete_item.WorkspaceName].push_back(ac); - } - } - } else { - list->push_back(autocomplete_item); - if (task_items) { - for (std::vector::const_iterator it = - (*task_items)[autocomplete_item.ProjectID].begin(); - it != (*task_items)[autocomplete_item.ProjectID].end(); ++it) { - view::Autocomplete ac = *it; - list->push_back(ac); - } - } + for (auto p : Projects) { + if (p->WID() != defaultWID) { + projectAutocompleteItem(unique_names, ws_names, list, items, task_items, p); } } } void RelatedData::TimeEntryAutocompleteItems( - std::vector *result) const { + std::vector *result, + const Poco::UInt64 defaultWID) const { std::set unique_names; std::map ws_names; std::map > items; workspaceAutocompleteItems(&unique_names, &ws_names, result); timeEntryAutocompleteItems(&unique_names, &ws_names, result, &items); - mergeGroupedAutocompleteItems(result, &items); + mergeGroupedAutocompleteItems(result, &items, &ws_names, defaultWID); } void RelatedData::MinitimerAutocompleteItems( - std::vector *result) const { + std::vector *result, + const Poco::UInt64 defaultWID) const { std::set unique_names; std::map ws_names; std::map > items; @@ -493,14 +513,16 @@ void RelatedData::MinitimerAutocompleteItems( workspaceAutocompleteItems(&unique_names, &ws_names, result); timeEntryAutocompleteItems(&unique_names, &ws_names, result, &items); taskAutocompleteItems(&unique_names, &ws_names, result, &task_items); - projectAutocompleteItems(&unique_names, &ws_names, result, &items, &task_items); + projectAutocompleteItems(&unique_names, &ws_names, result, &items, &task_items, defaultWID); - mergeGroupedAutocompleteItems(result, &items); + mergeGroupedAutocompleteItems(result, &items, &ws_names, defaultWID); } void RelatedData::mergeGroupedAutocompleteItems( std::vector *result, - std::map > *items) const { + std::map > *items, + std::map *ws_names, + const Poco::UInt64 defaultWID) const { // Join created workspace maps to a single vector Poco::UInt64 total_size = 0; for(std::map >::iterator iter = @@ -511,6 +533,16 @@ void RelatedData::mergeGroupedAutocompleteItems( result->reserve(total_size); + if (defaultWID) { + // add items from the default workspace to the top of the list + auto defaultWorkspaceName = (*ws_names)[defaultWID]; + auto defaultWorkspaceItems = items->find(defaultWorkspaceName); + if (defaultWorkspaceItems != items->end()) { + result->insert(result->end(), defaultWorkspaceItems->second.begin(), defaultWorkspaceItems->second.end()); + items->erase(defaultWorkspaceItems); + } + } + for(std::map >::iterator iter = items->begin(); iter != items->end(); ++iter) { @@ -520,13 +552,14 @@ void RelatedData::mergeGroupedAutocompleteItems( void RelatedData::ProjectAutocompleteItems( - std::vector *result) const { + std::vector *result, + const Poco::UInt64 defaultWID) const { std::set unique_names; std::map ws_names; std::map > task_items; workspaceAutocompleteItems(&unique_names, &ws_names, result); taskAutocompleteItems(&unique_names, &ws_names, result, &task_items); - projectAutocompleteItems(&unique_names, &ws_names, result, nullptr, &task_items); + projectAutocompleteItems(&unique_names, &ws_names, result, nullptr, &task_items, defaultWID); } void RelatedData::workspaceAutocompleteItems( diff --git a/src/related_data.h b/src/related_data.h index cf282f3d3c..cf4ba2b513 100644 --- a/src/related_data.h +++ b/src/related_data.h @@ -96,9 +96,9 @@ class TOGGL_INTERNAL_EXPORT RelatedData { std::string end_time, const Poco::UInt8 days_of_week); - void TimeEntryAutocompleteItems(std::vector *) const; - void MinitimerAutocompleteItems(std::vector *) const; - void ProjectAutocompleteItems(std::vector *) const; + void TimeEntryAutocompleteItems(std::vector *, const Poco::UInt64 defaultWID) const; + void MinitimerAutocompleteItems(std::vector *, const Poco::UInt64 defaultWID) const; + void ProjectAutocompleteItems(std::vector *, const Poco::UInt64 defaultWID) const; void ProjectLabelAndColorCode( TimeEntry * const te, @@ -125,13 +125,19 @@ class TOGGL_INTERNAL_EXPORT RelatedData { std::map *ws_names, std::vector *list, std::map > *items) const; + void projectAutocompleteItem(std::set* unique_names, + std::map* ws_names, + std::vector* list, + std::map>* items, + std::map>* task_items, Project* p) const; void projectAutocompleteItems( std::set *unique_names, std::map *ws_names, std::vector *list, std::map > *items, - std::map > *task_items) const; + std::map > *task_items, + const Poco::UInt64 defaultWID) const; void workspaceAutocompleteItems( std::set *unique_names, @@ -140,7 +146,9 @@ class TOGGL_INTERNAL_EXPORT RelatedData { void mergeGroupedAutocompleteItems( std::vector *result, - std::map > *items) const; + std::map > *items, + std::map *ws_names, + const Poco::UInt64 defaultWID) const; }; template<> inline TimeEntry *RelatedData::ModelByID(Poco::UInt64 id) { return TimeEntryByID(id); }