~dobey/unity-scope-click/result-attrs

« back to all changes in this revision

Viewing changes to scope/clickapps/apps-query.cpp

  • Committer: Tarmac
  • Author(s): Pawel Stolowski
  • Date: 2014-08-19 15:34:58 UTC
  • mfrom: (385.3.12 no-empty-departments)
  • Revision ID: tarmac-20140819153458-uo4b99al3z77dizp
Exclude empty departments from the departments tree in Apps. Fixes: https://bugs.launchpad.net/bugs/1350609.

Approved by PS Jenkins bot, Ted Gould, Rodney Dawes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
291
291
    searchReply->push(res);
292
292
}
293
293
 
294
 
void click::apps::Query::push_local_departments(scopes::SearchReplyProxy const& replyProxy)
 
294
void click::apps::Query::push_local_departments(scopes::SearchReplyProxy const& replyProxy, const std::vector<Application>& apps)
295
295
{
296
296
    auto const current_dep_id = query().department_id();
297
297
    const std::list<std::string> locales = { search_metadata().locale(), "en_US" };
298
298
 
 
299
    //
 
300
    // create helper lookup of all departments of currently installed apps.
 
301
    // note that apps that are passed here are supposed to be already filterd by current department
 
302
    // that means we only have subdepartments of current department (or subdepartment(s) of subdepartment(s)
 
303
    // and so on of current department, as the hierarchy may be of arbitrary depth.
 
304
    std::unordered_set<std::string> all_subdepartments;
 
305
    for (auto const app: apps)
 
306
    {
 
307
        all_subdepartments.insert(app.real_department);
 
308
    }
 
309
 
299
310
    unity::scopes::Department::SPtr root;
300
311
 
301
312
    try
309
320
        // attach subdepartments to it
310
321
        for (auto const& subdep: impl->depts_db->get_children_departments(current_dep_id))
311
322
        {
312
 
            // if single supdepartment fails, then ignore it and continue with others
313
 
            try
314
 
            {
315
 
                name = impl->depts_db->get_department_name(subdep.id, locales);
316
 
                unity::scopes::Department::SPtr dep = unity::scopes::Department::create(subdep.id, query(), name);
317
 
                dep->set_has_subdepartments(subdep.has_children);
318
 
                current->add_subdepartment(dep);
319
 
            }
320
 
            catch (const std::exception &e)
321
 
            {
322
 
                qWarning() << "Failed to create subdeparment:" << QString::fromStdString(e.what());
 
323
            //
 
324
            // check if this subdepartment either directly matches a subdepartment of installed app,
 
325
            // or is any of the departments of installed apps is a descendant of current subdepartment.
 
326
            // the latter means we have app somewhere in the subtree of the current subdepartment, so it
 
327
            // needs to be shown.
 
328
            bool show_subdepartment = false;
 
329
            auto it = all_subdepartments.find(subdep.id);
 
330
            if (it != all_subdepartments.end())
 
331
            {
 
332
                // subdepartment id matches directly one of the ids from all_subdepartments
 
333
                show_subdepartment = true;
 
334
                all_subdepartments.erase(it);
 
335
            }
 
336
            else
 
337
            {
 
338
                // no direct match - we need to check descendants of this subdepartment
 
339
                // by querying the db
 
340
                for (auto it = all_subdepartments.begin(); it != all_subdepartments.end(); it++)
 
341
                {
 
342
                    if (impl->depts_db->is_descendant_of_department(*it, subdep.id))
 
343
                    {
 
344
                        show_subdepartment = true;
 
345
                        all_subdepartments.erase(it);
 
346
                        break;
 
347
                    }
 
348
                }
 
349
            }
 
350
 
 
351
            if (show_subdepartment)
 
352
            {
 
353
                // if single supdepartment fails, then ignore it and continue with others
 
354
                try
 
355
                {
 
356
                    name = impl->depts_db->get_department_name(subdep.id, locales);
 
357
                    unity::scopes::Department::SPtr dep = unity::scopes::Department::create(subdep.id, query(), name);
 
358
                    dep->set_has_subdepartments(subdep.has_children);
 
359
                    current->add_subdepartment(dep);
 
360
                }
 
361
                catch (const std::exception &e)
 
362
                {
 
363
                    qWarning() << "Failed to create subdeparment:" << QString::fromStdString(e.what());
 
364
                }
323
365
            }
324
366
        }
325
367
 
357
399
    if (querystr.empty()) {
358
400
        if (impl->depts_db)
359
401
        {
360
 
            push_local_departments(searchReply);
 
402
            push_local_departments(searchReply, localResults);
361
403
        }
362
404
    }
363
405