aboutsummaryrefslogtreecommitdiff
path: root/0.8.0/src/gtkpaludis/browse_tree.cc
blob: fda7a3ce876cab737964cb5c8ab489fd9ada6e3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.org>
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "browse_tree.hh"
#include "information_tree.hh"
#include "main_window.hh"
#include "sync.hh"
#include "vtemm/reaper.hh"

#include <gtkmm/treestore.h>
#include <gtkmm/menu.h>
#include <gtkmm/messagedialog.h>

#include <paludis/environment/default/default_environment.hh>
#include <paludis/util/log.hh>
#include <paludis/syncer.hh>

#include <sys/types.h>
#include <sys/wait.h>
#include <iostream>

using namespace paludis;

namespace
{
    class BrowseTreeColumns;

    class BrowseTreeDisplayData :
        public InternalCounted<BrowseTreeDisplayData>
    {
        private:
            bool _has_children;

        protected:
            BrowseTreeDisplayData();

            virtual void load_children(
                    Glib::RefPtr<Gtk::TreeStore>,
                    const BrowseTreeColumns & columns,
                    Gtk::TreeModel::iterator) const = 0;

        public:
            virtual ~BrowseTreeDisplayData();

            virtual void display(InformationTree * const information_tree) const = 0;

            virtual void sync() const
            {
            }

            virtual void need_children(
                    Glib::RefPtr<Gtk::TreeStore> model,
                    const BrowseTreeColumns & columns,
                    Gtk::TreeModel::iterator us)
            {
                if (! _has_children)
                {
                    load_children(model, columns, us);
                    _has_children = true;
                }
            }

            class DefaultablePointer :
                public Pointer
            {
                public:
                    DefaultablePointer() :
                        Pointer(0)
                    {
                    }

                    template <typename T_>
                    DefaultablePointer(T_ v) :
                        Pointer(v)
                    {
                    }
            };
    };

    class BrowseTreeColumns :
        public Gtk::TreeModel::ColumnRecord
    {
        public:
            BrowseTreeColumns();
            ~BrowseTreeColumns();

            Gtk::TreeModelColumn<Glib::ustring> col_item;
            Gtk::TreeModelColumn<BrowseTreeDisplayData::DefaultablePointer> col_data;
    };

    class BrowseTreeDisplayTopData :
        public BrowseTreeDisplayData
    {
        protected:
            virtual void load_children(
                    Glib::RefPtr<Gtk::TreeStore>,
                    const BrowseTreeColumns &,
                    Gtk::TreeModel::iterator) const
            {
            }

        public:
            virtual void display(InformationTree * const information_tree) const
            {
                information_tree->show_top();
            }
    };

    class BrowseTreeDisplayVersionData :
        public BrowseTreeDisplayData
    {
        private:
            RepositoryName _r;
            QualifiedPackageName _q;
            VersionSpec _v;

        protected:
            virtual void load_children(
                    Glib::RefPtr<Gtk::TreeStore>,
                    const BrowseTreeColumns &,
                    Gtk::TreeModel::iterator) const
            {
            }

        public:
            BrowseTreeDisplayVersionData(const RepositoryName & r,
                    const QualifiedPackageName q, const VersionSpec & v) :
                _r(r),
                _q(q),
                _v(v)
            {
            }

            virtual void display(InformationTree * const information_tree) const
            {
                information_tree->show_version(_r, _q, _v);
            }
    };

    class BrowseTreeDisplayPackageData :
        public BrowseTreeDisplayData
    {
        private:
            RepositoryName _r;
            QualifiedPackageName _q;

        protected:
            virtual void load_children(
                    Glib::RefPtr<Gtk::TreeStore> model,
                    const BrowseTreeColumns & columns,
                    Gtk::TreeModel::iterator us) const
            {
                Repository::ConstPointer repo(
                        DefaultEnvironment::get_instance()->package_database()->fetch_repository(_r));

                VersionSpecCollection::ConstPointer vers(repo->version_specs(_q));
                for (VersionSpecCollection::Iterator ver(vers->begin()), ver_end(vers->end()) ;
                        ver != ver_end ; ++ver)
                {
                    Gtk::TreeModel::Row ver_row = *(model->append(us->children()));
                    ver_row[columns.col_item] = stringify(*ver);
                    ver_row[columns.col_data] = BrowseTreeDisplayData::Pointer(
                            new BrowseTreeDisplayVersionData(_r, _q, *ver));
                }
            }

        public:
            BrowseTreeDisplayPackageData(const RepositoryName & r,
                    const QualifiedPackageName q) :
                _r(r),
                _q(q)
            {
            }

            virtual void display(InformationTree * const information_tree) const
            {
                information_tree->show_package(_r, _q);
            }
    };

    class BrowseTreeDisplayCategoryData :
        public BrowseTreeDisplayData
    {
        private:
            RepositoryName _r;
            CategoryNamePart _c;

        protected:
            virtual void load_children(
                    Glib::RefPtr<Gtk::TreeStore> model,
                    const BrowseTreeColumns & columns,
                    Gtk::TreeModel::iterator us) const
            {
                Repository::ConstPointer repo(
                        DefaultEnvironment::get_instance()->package_database()->fetch_repository(_r));

                QualifiedPackageNameCollection::ConstPointer pkgs(repo->package_names(_c));
                for (QualifiedPackageNameCollection::Iterator pkg(pkgs->begin()), pkg_end(pkgs->end()) ;
                        pkg != pkg_end ; ++pkg)
                {
                    Gtk::TreeModel::Row pkg_row = *(model->append(us->children()));
                    pkg_row[columns.col_item] = stringify(pkg->package);
                    pkg_row[columns.col_data] = BrowseTreeDisplayData::Pointer(
                            new BrowseTreeDisplayPackageData(_r, *pkg));
                }
            }

        public:
            BrowseTreeDisplayCategoryData(const RepositoryName & r,
                    const CategoryNamePart c) :
                _r(r),
                _c(c)
            {
            }

            virtual void display(InformationTree * const information_tree) const
            {
                information_tree->show_category(_r, _c);
            }
    };

    class BrowseTreeDisplayRepositoryData :
        public BrowseTreeDisplayData
    {
        private:
            RepositoryName _r;

        protected:
            virtual void load_children(
                    Glib::RefPtr<Gtk::TreeStore> model,
                    const BrowseTreeColumns & columns,
                    Gtk::TreeModel::iterator us) const
            {
                Repository::ConstPointer repo(
                        DefaultEnvironment::get_instance()->package_database()->fetch_repository(_r));

                CategoryNamePartCollection::ConstPointer cats(repo->category_names());
                for (CategoryNamePartCollection::Iterator cat(cats->begin()), cat_end(cats->end()) ;
                        cat != cat_end ; ++cat)
                {
                    Gtk::TreeModel::Row cat_row = *(model->append(us->children()));
                    cat_row[columns.col_item] = stringify(*cat);
                    cat_row[columns.col_data] = BrowseTreeDisplayData::Pointer(
                            new BrowseTreeDisplayCategoryData(_r, *cat));
                }
            }

        public:
            BrowseTreeDisplayRepositoryData(const RepositoryName & r) :
                _r(r)
            {
            }

            virtual void display(InformationTree * const information_tree) const
            {
                information_tree->show_repository(_r);
            }

            virtual void sync() const
            {
                OurSyncTask task;
                task.add_target(stringify(_r));
                task.execute();
            }

    };
}

BrowseTreeDisplayData::BrowseTreeDisplayData() :
    _has_children(false)
{
}

BrowseTreeDisplayData::~BrowseTreeDisplayData()
{
}


BrowseTreeColumns::BrowseTreeColumns()
{
    add(col_item);
    add(col_data);
}

BrowseTreeColumns::~BrowseTreeColumns()
{
}

namespace paludis
{
    template<>
    struct Implementation<BrowseTree> :
        InternalCounted<Implementation<BrowseTree> >
    {
        MainWindow * const main_window;
        InformationTree * const information_tree;

        BrowseTreeColumns columns;
        Glib::RefPtr<Gtk::TreeStore> model;

        Gtk::Menu popup_menu;

        pid_t paludis_child;

        Implementation(MainWindow * const m, InformationTree * const i) :
            main_window(m),
            information_tree(i),
            paludis_child(-1)
        {
        }

        virtual ~Implementation()
        {
        }
    };
}

BrowseTree::BrowseTree(MainWindow * const main_window,
        InformationTree * const information_tree) :
    PrivateImplementationPattern<BrowseTree>(new Implementation<BrowseTree>(
                main_window, information_tree))
{
    _imp->model = Gtk::TreeStore::create(_imp->columns);
    set_model(_imp->model);

    Gtk::TreeModel::Row repositories_row = *(_imp->model->append());
    repositories_row[_imp->columns.col_item] = "Repositories";
    repositories_row[_imp->columns.col_data] = BrowseTreeDisplayData::DefaultablePointer(
            new BrowseTreeDisplayTopData);

    for (PackageDatabase::RepositoryIterator
            r(DefaultEnvironment::get_instance()->package_database()->begin_repositories()),
            r_end(DefaultEnvironment::get_instance()->package_database()->end_repositories()) ;
            r != r_end ; ++r)
    {
        Gtk::TreeModel::Row repository_row = *(_imp->model->append(repositories_row.children()));
        repository_row[_imp->columns.col_item] = stringify((*r)->name());
        repository_row[_imp->columns.col_data] = BrowseTreeDisplayData::Pointer(
                new BrowseTreeDisplayRepositoryData((*r)->name()));
    }

    append_column("Item", _imp->columns.col_item);
    get_selection()->signal_changed().connect(sigc::mem_fun(*this, &BrowseTree::on_changed));
    Vte::Reaper::get_instance()->signal_child_exited().connect(sigc::mem_fun(*this, &BrowseTree::on_child_process_exited));

    /* popup menu */
    _imp->popup_menu.items().push_back(Gtk::Menu_Helpers::MenuElem("_Sync",
                sigc::mem_fun(*this, &BrowseTree::on_menu_sync)));
    _imp->popup_menu.accelerate(*this);
}

BrowseTree::~BrowseTree()
{
}

void
BrowseTree::on_changed()
{
    Gtk::TreeModel::iterator i(get_selection()->get_selected());
    if (i)
    {
        Gtk::TreeModel::Row row(*i);
        BrowseTreeDisplayData::Pointer(row[_imp->columns.col_data])->need_children(_imp->model,
                _imp->columns, i);
        BrowseTreeDisplayData::Pointer(row[_imp->columns.col_data])->display(
                _imp->information_tree);
    }
}

void
BrowseTree::on_menu_sync()
{
    Glib::RefPtr<Gtk::TreeView::Selection> selection(get_selection());
    if (selection)
    {
        Gtk::TreeModel::iterator i(selection->get_selected());
        if (i)
        {
            _imp->main_window->set_children_sensitive(false);

            pid_t child(fork());
            if (0 == child)
            {
                try
                {
                    BrowseTreeDisplayData::Pointer((*i)[_imp->columns.col_data])->sync();
                    _exit(0);
                }
                catch (const SyncFailedError & e)
                {
                    _exit(1);
                }
            }
            else if (-1 == child)
                throw InternalError(PALUDIS_HERE, "fork failed");
            else
            {
                Log::get_instance()->message(ll_debug, lc_no_context,
                        "Forked child process " + stringify(child));
                _imp->paludis_child = child;
                Vte::Reaper::get_instance()->add_child(child);
            }
        }
    }
}

bool
BrowseTree::on_button_press_event(GdkEventButton * our_event)
{
    bool result(TreeView::on_button_press_event(our_event));

    if (our_event->type == GDK_BUTTON_PRESS && our_event->button == 3)
        _imp->popup_menu.popup(our_event->button, our_event->time);

    return result;
}

void
BrowseTree::on_child_process_exited(int, int status)
{
    if (-1 == _imp->paludis_child)
        return;

    if (0 == status)
        Log::get_instance()->message(ll_debug, lc_no_context, "child " + stringify(_imp->paludis_child)
                 + " exited with success");
    else
        Log::get_instance()->message(ll_debug, lc_no_context, "child " + stringify(_imp->paludis_child)
                 + " exited with failure code " + stringify(status));

    _imp->paludis_child = -1;
    _imp->main_window->set_children_sensitive(true);
}