~ubuntu-branches/ubuntu/natty/unity-2d/natty-updates

« back to all changes in this revision

Viewing changes to places/UnityPlaces/windowslist.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Casadevall
  • Date: 2011-01-14 17:42:08 UTC
  • Revision ID: james.westby@ubuntu.com-20110114174208-ww045t7shnu3a5zv
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2010 Canonical, Ltd.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; version 3.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 */
 
16
 
 
17
#include <QDebug>
 
18
 
 
19
#include "windowslist.h"
 
20
#include "windowinfo.h"
 
21
 
 
22
#include "bamf-matcher.h"
 
23
#include "bamf-window.h"
 
24
#include "bamf-application.h"
 
25
 
 
26
WindowsList::WindowsList(QObject *parent) :
 
27
    QAbstractListModel(parent), m_applicationId(0),
 
28
    m_loaded(false), m_lastActiveWindow(NULL)
 
29
{
 
30
    QHash<int, QByteArray> roles;
 
31
    roles[0] = "window";
 
32
    setRoleNames(roles);
 
33
}
 
34
 
 
35
WindowsList::~WindowsList()
 
36
{
 
37
    qDeleteAll(m_windows);
 
38
}
 
39
 
 
40
int WindowsList::rowCount(const QModelIndex &parent) const
 
41
{
 
42
    Q_UNUSED(parent)
 
43
 
 
44
    return m_windows.size();
 
45
}
 
46
 
 
47
QVariant WindowsList::data(const QModelIndex &index, int role) const
 
48
{
 
49
    Q_UNUSED(role);
 
50
 
 
51
    if (!index.isValid())
 
52
        return QVariant();
 
53
 
 
54
    WindowInfo *info = m_windows.at(index.row());
 
55
    return QVariant::fromValue(info);
 
56
}
 
57
 
 
58
void WindowsList::load(unsigned long applicationId)
 
59
{
 
60
    if (m_loaded && m_applicationId == applicationId) {
 
61
        return;
 
62
    }
 
63
 
 
64
    BamfMatcher &matcher = BamfMatcher::get_default();
 
65
    QList<BamfApplication*> applications;
 
66
 
 
67
    if (applicationId == 0) {
 
68
        /* List the windows of all the applications */
 
69
        BamfApplicationList *allapplications = matcher.applications();
 
70
        for (int i = 0; i < allapplications->size(); i++) {
 
71
             applications.append(allapplications->at(i));
 
72
        }
 
73
    } else {
 
74
        /* List the windows of the application that has for group leader the window
 
75
           with XID applicationId */
 
76
        applications.append(matcher.application_for_xid(applicationId));
 
77
    }
 
78
 
 
79
    BamfWindow* activeWindow = matcher.active_window();
 
80
 
 
81
    /* Build a list of windowInfos for windows that are 'user_visible' according to BAMF */
 
82
    QList<WindowInfo*> newWindows;
 
83
 
 
84
    Q_FOREACH (BamfApplication* application, applications) {
 
85
        if (!application->user_visible()) {
 
86
            continue;
 
87
        }
 
88
 
 
89
        BamfWindowList *bamfWindows = application->windows();
 
90
        for (int i = 0; i < bamfWindows->size(); i++) {
 
91
            BamfWindow* window = bamfWindows->at(i);
 
92
            if (!window->user_visible()) {
 
93
                continue;
 
94
            }
 
95
 
 
96
            WindowInfo *info = new WindowInfo(window->xid());
 
97
            newWindows.append(info);
 
98
 
 
99
            if (activeWindow != NULL && window->xid() == activeWindow->xid()) {
 
100
                m_lastActiveWindow = info;
 
101
            }
 
102
        }
 
103
    }
 
104
 
 
105
    qDebug() << "Matched" << newWindows.count() << "Windows in" << applications.count() << "Applications";
 
106
 
 
107
    if (m_windows.count() > 0) {
 
108
        beginRemoveRows(QModelIndex(), 0, m_windows.count() - 1);
 
109
        qDeleteAll(m_windows);
 
110
        m_windows.clear();
 
111
        endRemoveRows();
 
112
    }
 
113
 
 
114
    if (newWindows.count() > 0) {
 
115
        beginInsertRows(QModelIndex(), 0, newWindows.count() - 1);
 
116
        m_windows.append(newWindows);
 
117
        endInsertRows();
 
118
    }
 
119
 
 
120
    m_applicationId = applicationId;
 
121
    m_loaded = true;
 
122
 
 
123
    Q_EMIT countChanged(m_windows.count());
 
124
    Q_EMIT lastActiveWindowChanged(m_lastActiveWindow);
 
125
}
 
126
 
 
127
void WindowsList::unload()
 
128
{
 
129
    beginRemoveRows(QModelIndex(), 0, m_windows.count() - 1);
 
130
    qDeleteAll(m_windows);
 
131
    m_windows.clear();
 
132
    endRemoveRows();
 
133
 
 
134
    m_loaded = false;
 
135
 
 
136
    Q_EMIT countChanged(m_windows.count());
 
137
}
 
138
 
 
139
WindowInfo* WindowsList::lastActiveWindow() const {
 
140
    return m_lastActiveWindow;
 
141
}