~ubuntu-branches/ubuntu/maverick/padre/maverick

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
package Padre::Config::Host;

# Configuration and state data related to the host that Padre is running on.

use 5.008;
use strict;
use warnings;
use Padre::Current ();

our $VERSION = '0.63';

# -- constructors

#
# my $config = Padre::Config::Host->_new( $href );
#
# create & return a new config object. if $href is not supplied, the config
# object will be empty. this constructor is private and should not be used
# outside this class.
#
sub _new {
	my ( $class, $href ) = @_;
	$href ||= {};
	bless $href, $class;
	return $href;
}

#
# my $config = Padre::Config::Host->read;
#
sub read {
	my $class = shift;

	# Read in the config data
	require Padre::DB;
	my %hash = map { $_->name => $_->value } Padre::DB::HostConfig->select;

	# Create and return the object
	return $class->_new( \%hash );
}

# -- public methods

#
# my $revision = $config->version;
#
sub version {
	my $self = shift;
	$self->{version}; # stored as other preferences!
}

#
# $config->write;
#
sub write {
	my $self = shift;
	require Padre::DB;

	# This code can run before we have a ::Main object.
	# As a result, it uses slightly bizarre locking code to make sure it runs
	# inside a transaction correctly in both cases (has a ::Main, or not)
	my $main = eval {

		# If ::Main isn't even loaded, we don't need the more
		# intensive Padre::Current call. It also prevents loading
		# the Wx subsystem when we are running light and headless
		# with no GUI at all.
		if ($Padre::Wx::Main::VERSION) {
			local $@;
			Padre::Current->main;
		}
	};
	my $lock = $main ? $main->lock('DB') : undef;
	Padre::DB->begin unless $lock;
	Padre::DB::HostConfig->truncate;
	foreach my $name ( sort keys %$self ) {
		Padre::DB::HostConfig->create(
			name  => $name,
			value => $self->{$name},
		);
	}
	Padre::DB->commit unless $lock;

	return 1;
}

1;

__END__

=pod

=head1 NAME

Padre::Config::Host - Padre configuration storing host state data

=head1 DESCRIPTION

This class implements the state data of the host on which Padre is running.
See L<Padre::Config> for more information on the various types of preferences
supported by Padre.

All those state data are stored in a database managed with C<Padre::DB>.
Refer to this module for more information on how this works.

=head1 PUBLIC API

=head2 Constructors

=over 4

=item read

    my $config = Padre::Config::Host->read;

Load & return the host configuration from the database. Return C<undef> in
case of failure.

No parameters.

=back

=head2 Object methods

=over 4

=item version

    my $revision = $config->version;

Return the configuration schema revision. Indeed, we might want to change the
underlying storage later on.

No parameters.

=item write

    $config->write;

(Over-)write host configuration to the database.

No parameters.

=back

=head1 COPYRIGHT & LICENSE

Copyright 2008-2010 The Padre development team as listed in Padre.pm.

This program is free software; you can redistribute it and/or modify it under the
same terms as Perl 5 itself.

=cut

# Copyright 2008-2010 The Padre development team as listed in Padre.pm.
# LICENSE
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl 5 itself.