| 1 | # Copyright 2007, 2008, 2009, 2010, 2011 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Chart. | ||
| 4 | # | ||
| 5 | # Chart is free software; you can redistribute it and/or modify it under the | ||
| 6 | # terms of the GNU General Public License as published by the Free Software | ||
| 7 | # Foundation; either version 3, or (at your option) any later version. | ||
| 8 | # | ||
| 9 | # Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 10 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU General Public License along | ||
| 15 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | package App::Chart::Gtk2::Symlist::User; | ||
| 18 | use strict; | ||
| 19 | use warnings; | ||
| 20 | use Carp; | ||
| 21 | use List::Util qw(min max); | ||
| 22 | # use Locale::TextDomain ('App-Chart'); | ||
| 23 | |||
| 24 | |||
| 25 | use App::Chart::Gtk2::Symlist; | ||
| 26 | use Glib::Object::Subclass | ||
| 27 | 'App::Chart::Gtk2::Symlist'; | ||
| 28 | |||
| 29 | sub can_edit { return 1; } | ||
| 30 | sub can_delete_symlist { return 1; } | ||
| 31 | |||
| 32 | sub delete_symlist { | ||
| 33 | my ($self) = @_; | ||
| 34 | my $key = $self->{'key'}; | ||
| 35 | require App::Chart::DBI; | ||
| 36 | my $dbh = App::Chart::DBI->instance; | ||
| 37 | my $seq; | ||
| 38 | App::Chart::Database::call_with_transaction | ||
| 39 | ($dbh, sub { | ||
| 40 | $seq = App::Chart::Database::read_notes_single | ||
| 41 | ('SELECT seq FROM symlist WHERE key=?', $key); | ||
| 42 | $dbh->do ('DELETE FROM symlist WHERE key=?', undef, $key); | ||
| 43 | $dbh->do ('DELETE FROM symlist_content WHERE key=?', undef, $key); | ||
| 44 | if (defined $seq) { | ||
| 45 | $dbh->do ('UPDATE symlist SET seq=-seq WHERE seq>?', undef, $seq); | ||
| 46 | $dbh->do ('UPDATE symlist SET seq=-seq-1 WHERE seq<0'); | ||
| 47 | } | ||
| 48 | { local $self->{'reading_database'} = 1; | ||
| 49 | $self->clear; | ||
| 50 | } | ||
| 51 | }); | ||
| 52 | App::Chart::chart_dirbroadcast()->send ('symlist-list-deleted', $seq); | ||
| 53 | } | ||
| 54 | |||
| 55 | sub add_symlist { | ||
| 56 | my ($class, $pos, $name) = @_; | ||
| 57 | require App::Chart::Gtk2::SymlistListModel; | ||
| 58 | my $list = App::Chart::Gtk2::SymlistListModel->instance; | ||
| 59 | require App::Chart::DBI; | ||
| 60 | my $dbh = App::Chart::DBI->instance; | ||
| 61 | my $key; | ||
| 62 | App::Chart::Database::call_with_transaction | ||
| 63 | ($dbh, sub { | ||
| 64 | $key = new_user_symlist_key($dbh); | ||
| 65 | $list->insert_with_values ($pos, | ||
| 66 | $list->COL_KEY => $key, | ||
| 67 | $list->COL_NAME => $name); | ||
| 68 | }); | ||
| 69 | return $key; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub new_user_symlist_key { | ||
| 73 | for (my $i = 1; $i < 1_000_000; $i++) { | ||
| 74 | my $key = "user-$i"; | ||
| 75 | if (! App::Chart::Database::read_notes_single | ||
| 76 | ('SELECT key FROM symlist WHERE key=?', $key)) { | ||
| 77 | return $key; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | die "Oops, cannot find new user symlist key"; | ||
| 81 | } | ||
| 82 | |||
| 83 | 1; | ||
| 84 | __END__ |