comparison doc/PERL-HOWTO.dox @ 15065:e53cceebebb3

[gaim-migrate @ 17849] Update the Perl HOWTO to include working samples and various other fixes. In order to actually do this, I had to also make some fixes to the perl bindings. committer: Tailor Script <tailor@pidgin.im>
author Daniel Atallah <daniel.atallah@gmail.com>
date Wed, 29 Nov 2006 23:46:55 +0000
parents dd0c43d42394
children 3d6f2568457c
comparison
equal deleted inserted replaced
15064:252c0b69813b 15065:e53cceebebb3
1 /** @page perl-howto Perl Scripting HOWTO 1 /** @page perl-howto Perl Scripting HOWTO
2 2
3 @section Introduction 3 @section Introduction
4 Gaim Perl Plugins are setup very similarly to their C counterparts. Most of the API calls are implemented and are divided into pacakges. There are some significant differences between the Perl and C API. Much like the C API, the best place to seek guidances is the source located in the plugins/perl/common directory. The tutorial that follows will be example based and attempt to touch on the salient features of the embedded perl interpreter. It is also important to note that some of the C API is missing in Gaim's perl API. 4 Gaim Perl Plugins are setup very similarly to their C counterparts. Most of the API calls are implemented and are divided into pacakges. There are some significant differences between the Perl and C API. Much like the C API, the best place to seek guidances is the source located in the plugins/perl/common directory. The tutorial that follows will be example based and attempt to touch on the salient features of the embedded perl interpreter. It is also important to note that some of the C API is missing in Gaim's perl API.
5 5
6 It is possible to get Gtk2-Perl to work with Gaim's perl API, however you must not load the module with @c use but with @c require. If you are uninterested in using Gtk with your perl plugins than this still has bearing on you if you would like to use certain perl modules that are dynamically loaded. By always using @c require instead of @c use the problem is avoided. 6 It is possible to get Gtk2-Perl to work with Gaim's perl API, however you must not load the module with @c use but with @c require. If you are uninterested in using Gtk with your perl plugins than this still has bearing on you if you would like to use certain perl modules that are dynamically loaded. By always using @c require instead of @c use the problem is avoided.
7 7
8 @section first-script Writing your first script 8 @section first-script Writing your first script
9 9
10 Let us start with a simple example of a Gaim perl plugin. The following code sample is a complete plugin that can be copied and used as is. 10 Let us start with a simple example of a Gaim perl plugin. The following code sample is a complete plugin that can be copied and used as is.
11 11
12 @code 12 @code
13 use Gaim; 13 use Gaim;
14 14
15 %PLUGIN_INFO = ( 15 %PLUGIN_INFO = (
16 perl_api_version => 2, 16 perl_api_version => 2,
17 name => "Perl Test Plugin", 17 name => "Perl Test Plugin",
18 version => "0.1", 18 version => "0.1",
19 summary => "Test plugin for the Perl interpreter.", 19 summary => "Test plugin for the Perl interpreter.",
20 description => "Your description here", 20 description => "Your description here",
21 author => "John H. Kelm <johnhkelm\@gmail.com", 21 author => "John H. Kelm <johnhkelm\@gmail.com",
22 url => "http://gaim.sourceforge.net/", 22 url => "http://gaim.sourceforge.net/",
23 23
24 load => "plugin_load", 24 load => "plugin_load",
25 unload => "plugin_unload" 25 unload => "plugin_unload"
26 ); 26 );
27 27
28 sub plugin_init { 28 sub plugin_init {
29 return %PLUGIN_INFO; 29 return %PLUGIN_INFO;
30 } 30 }
31 31
32 sub plugin_load { 32 sub plugin_load {
33 my $plugin = shift; 33 my $plugin = shift;
34 Gaim::debug_info("plugin_load()", "Test Plugin Loaded."); 34 Gaim::Debug::info("testplugin", "plugin_load() - Test Plugin Loaded.\n");
35 } 35 }
36 36
37 sub plugin_unload { 37 sub plugin_unload {
38 my $plugin = shift; 38 my $plugin = shift;
39 Gaim::debug_info("plugin_unload()", "Test Plugin Unloaded."); 39 Gaim::Debug::info("testplugin", "plugin_unload() - Test Plugin Unloaded.\n");
40 } 40 }
41 @endcode 41 @endcode
42 42
43 It is necessary to load the Gaim perl package with the line @code use Gaim; @endcode which will make all the Gaim perl API available to the script. The @c \%PLUGIN_INFO has contains all the information that will be displayed in the Plugin frame of the Preferences dialog. In addition to information needed to describe the plugin to the user, information about how the plugin is to be handled is present. The keys @c load and @c unload specify and action to take when the plugin is loaded and when it is unloaded from the Preferences dialog respectively. There are other key values that may be present in the @c \%PLUGIN_INFO hash that will be covered in the following sections. 43 It is necessary to load the libgaim perl package with the line @code use Gaim; @endcode which will make all the libgaim perl API available to the script. The @c \%PLUGIN_INFO has contains all the information that will be displayed in the Plugin frame of the Preferences dialog. In addition to information needed to describe the plugin to the user, information about how the plugin is to be handled is present. The keys @c load and @c unload specify and action to take when the plugin is loaded and when it is unloaded from the Preferences dialog respectively. There are other key values that may be present in the @c \%PLUGIN_INFO hash that will be covered in the following sections.
44 44
45 The Perl subroutine @c plugin_init is executed when the plugin is probed by the plugin subsystem. What this means is as soon as Gaim is started, this subroutine is run once, regardless of whether the plugin is loaded or not. The other two subroutines present are those defined by the @c \%PLUGIN_INFO hash and take the plugin handle as an argument. When the plugin is loaded and subsequently unloaded it will print a message to the debug window using the @c Gaim::debug_info() Gaim perl API call. 45 The Perl subroutine @c plugin_init is executed when the plugin is probed by the plugin subsystem. What this means is as soon as Gaim is started, this subroutine is run once, regardless of whether the plugin is loaded or not. The other two subroutines present are those defined by the @c \%PLUGIN_INFO hash and take the plugin handle as an argument. When the plugin is loaded and subsequently unloaded it will print a message to the debug window using the @c Gaim::Debug::info() Gaim perl API call.
46 46
47 The last step is to save the script with a .pl file extention in your ~/.gaim/plugins directory. After restarting gaim the plugin "Perl Test Plugin" should now appear under "Tools -> Preferences -> Plugins". To view the messages make sure you run Gaim from the console with the '-d' flag or open the Debug Window from inside Gaim under "Help". When you enable the checkbox next the plugin you should see a message appear in the Debug Window (or console) and when you disable the checkbox you should see another message appear. You have now created the framework that will allow you to create almost any kind of Gaim plugin you can imagine. 47 The last step is to save the script with a .pl file extention in your ~/.gaim/plugins directory. After restarting gaim the plugin "Perl Test Plugin" should now appear under "Tools -> Plugins". To view the messages make sure you run Gaim from the console with the '-d' flag or open the Debug Window from inside Gaim under "Help". When you enable the checkbox next the plugin you should see a message appear in the Debug Window (or console) and when you disable the checkbox you should see another message appear. You have now created the framework that will allow you to create almost any kind of Gaim plugin you can imagine.
48 48
49 @section account-api Account and Account Option Functions 49 @section account-api Account and Account Option Functions
50 50
51 The Account API is in the @c Gaim::Account:: and @c Gaim::Accounts:: packages 51 The Account API is in the @c Gaim::Account:: and @c Gaim::Accounts:: packages
52 and both are nearly identical to their C counterparts @c gaim_account_ and @c 52 and both are nearly identical to their C counterparts @c gaim_account_ and @c
74 sub plugin_load { 74 sub plugin_load {
75 $plugin = shift; 75 $plugin = shift;
76 76
77 # Testing was done using Oscar, but this should work regardless of the protocol chosen 77 # Testing was done using Oscar, but this should work regardless of the protocol chosen
78 my $protocol = "prpl-oscar"; 78 my $protocol = "prpl-oscar";
79 my $account_name = "test";
79 80
80 # Create a new Account 81 # Create a new Account
81 print "Testing: Gaim::Account::new()... "; 82 print "Testing: Gaim::Account::new()... ";
82 $account = Gaim::Account->new($TEST_NAME, $PROTOCOL_ID); 83 $account = Gaim::Account->new($account_name, $protocol);
83 if ($account) { print "ok.\n"; } else { print "fail.\n"; } 84 if ($account) { print "ok.\n"; } else { print "fail.\n"; }
84 85
85 # Add a new Account 86 # Add a new Account
86 print "Testing: Gaim::Account::add()..."; 87 print "Testing: Gaim::Account::add()...";
87 Gaim::Accounts::add($account); 88 Gaim::Accounts::add($account);
88 print "pending find...\n"; 89 print "pending find...\n";
89 90
90 # Find the account we just added to verify its existence 91 # Find the account we just added to verify its existence
91 print "Testing: Gaim::Accounts::find()..."; 92 print "Testing: Gaim::Accounts::find()...";
92 $account = Gaim::Accounts::find("TEST_NAME", $protocol); 93 $account = Gaim::Accounts::find($account_name, $protocol);
93 if ($account) { print "ok.\n"; } else { print "fail.\n"; } 94 if ($account) { print "ok.\n"; } else { print "fail.\n"; }
94 95
95 # Return the username 96 # Return the username
96 print "Testing: Gaim::Account::get_username()... "; 97 print "Testing: Gaim::Account::get_username()... ";
97 $user_name = $account->get_username(); 98 $user_name = $account->get_username();
98 if ($user_name) { 99 if ($user_name) {
99 print "Success: $user_name.\n"; 100 print "Success: $user_name.\n";
100 } else { 101 } else {
120 121
121 # It follows that to connect a user you mest set the account status to 122 # It follows that to connect a user you mest set the account status to
122 # "available" similarly we can disconnect a user by setting the account 123 # "available" similarly we can disconnect a user by setting the account
123 # status to "offline" 124 # status to "offline"
124 125
125 $account = Gaim::Accounts::find("TEST_NAME", $protocol); 126 print "Testing: Gaim::Accounts::connect()...pending...\n";
126 print "Testing: Gaim::Accounts::connect()...pending...\n";
127 127
128 $account->set_status("available", TRUE); 128 $account->set_status("available", TRUE);
129 $account->set_enabled(Gaim::Core::get_ui(), TRUE);
129 $account->connect(); 130 $account->connect();
130 131
131 } 132 }
132 @endcode 133 @endcode
133 134
139 @c Gaim::Account::get_username(). For arguments that you would make @c NULL in 140 @c Gaim::Account::get_username(). For arguments that you would make @c NULL in
140 C should be set to @c undef in Perl. 141 C should be set to @c undef in Perl.
141 142
142 @section buddylist-api Buddylist, Group and Chat API 143 @section buddylist-api Buddylist, Group and Chat API
143 144
144 The BuddList, Group and Chat APIs are very similar and whatever is shown for the @c Gaim::BuddlyList API should carry over to @c Gaim::Chat and @c Gaim::Group. Note that there is a @c Gaim::Find pacakge that was created to keep the naming consistent with how these functions are named in the C API. 145 The BuddyList, Group and Chat APIs are very similar and whatever is shown for the @c Gaim::BuddyList API should carry over to @c Gaim::BuddyList::Chat and @c Gaim::BuddyList::Group. Note that there is a @c Gaim::Find package that was created to keep the naming consistent with how these functions are named in the C API.
145 146
146 @code 147 @code
147 sub plugin_load { 148 sub plugin_load {
148 my $plugin = shift; 149 my $plugin = shift;
150
149 my $protocol = "prpl-oscar"; 151 my $protocol = "prpl-oscar";
150 152 my $account_name = "test";
151 # This is how we get an account to use in the following tests. You should replace the username 153
152 # with an existent user 154 # This is how we get an account to use in the following tests. You should replace the username
153 $account = Gaim::Accounts::find("USERNAME", $protocol); 155 # with an existing user
156 $account = Gaim::Accounts::find($account_name, $protocol);
154 157
155 # Testing a find function: Note Gaim::Find not Gaim::Buddy:find! 158 # Testing a find function: Note Gaim::Find not Gaim::Buddy:find!
156 # Furthermore, this should work the same for chats and groups 159 # Furthermore, this should work the same for chats and groups
157 print "Testing: Gaim::Find::buddy()..."; 160 Gaim::Debug::info("testplugin", "Testing: Gaim::Find::buddy()...");
158 $buddy = Gaim::Find::buddy($account, "BUDDYNAME"); 161 $buddy = Gaim::Find::buddy($account, "BUDDYNAME");
159 if ($buddy) { print "ok.\n"; } else { print "fail.\n"; } 162 Gaim::Debug::info("", ($buddy ? "ok." : "fail.") . "\n");
160 163
161 # If you should need the handle for some reason, here is how you do it 164 # If you should need the handle for some reason, here is how you do it
162 print "Testing: Gaim::BuddyList::get_handle()..."; 165 Gaim::Debug::info("testplugin", "Testing: Gaim::BuddyList::get_handle()...");
163 $handle = Gaim::BuddyList::get_handle(); 166 $handle = Gaim::BuddyList::get_handle();
164 if ($handle) { print "ok.\n"; } else { print "fail.\n"; } 167 Gaim::Debug::info("", ($handle ? "ok." : "fail.") . "\n");
165 168
166 # This gets the Gaim::BuddyList and references it by $blist 169 # This gets the Gaim::BuddyList and references it by $blist
167 print "Testing: Gaim::BuddyList::get_blist()..."; 170 Gaim::Debug::info("testplugin", "Testing: Gaim::get_blist()...");
168 $blist = Gaim::BuddyList::get_blist(); 171 $blist = Gaim::get_blist();
169 if ($blist) { print "ok.\n"; } else { print "fail.\n"; } 172 Gaim::Debug::info("", ($blist ? "ok." : "fail.") . "\n");
170 173
171 # This is how you would add a buddy named "NEWNAME" with the alias "ALIAS" 174 # This is how you would add a buddy named "NEWNAME" with the alias "ALIAS"
172 print "Testing: Gaim::Buddy::new..."; 175 Gaim::Debug::info("testplugin", "Testing: Gaim::BuddyList::Buddy::new...");
173 $buddy = Gaim::Buddy::new($account, "NEWNAME", "ALIAS"); 176 $buddy = Gaim::BuddyList::Buddy::new($account, "NEWNAME", "ALIAS");
174 if ($buddy) { print "ok.\n"; } else { print "fail.\n"; } 177 Gaim::Debug::info("", ($buddy ? "ok." : "fail.") . "\n");
175 178
176 # Here we add the new buddy '$buddy' to the group "GROUP" 179 # Here we add the new buddy '$buddy' to the group "GROUP"
177 # so first we must find the group 180 # so first we must find the group
178 print "Testing: Gaim::Find::group..."; 181 Gaim::Debug::info("testplugin", "Testing: Gaim::Find::group...");
179 $group = Gaim::Find::group("GROUP"); 182 $group = Gaim::Find::group("GROUP");
180 if ($group) { print "ok.\n"; } else { print "fail.\n"; } 183 Gaim::Debug::info("", ($group ? "ok." : "fail.") . "\n");
181 184
182 # To add the buddy we need to have the buddy, contact, group and node for insertion. 185 # To add the buddy we need to have the buddy, contact, group and node for insertion.
183 # For this example we can let contact be undef and set the insertion node as the group 186 # For this example we can let contact be undef and set the insertion node as the group
184 print "Testing: Gaim::BuddyList::add_buddy..."; 187 Gaim::Debug::info("testplugin", "Testing: Gaim::BuddyList::add_buddy...\n");
185 Gaim::BuddyList::add_buddy($buddy, undef, $group, $group); 188 Gaim::BuddyList::add_buddy($buddy, undef, $group, $group);
186 if ($buddy) { print "ok.\n"; } else { print "fail.\n"; }
187 189
188 # The example that follows gives an indiction of how an API call that returns a list is handled. 190 # The example that follows gives an indiction of how an API call that returns a list is handled.
189 # In this case the buddies of the account found earlier are retrieved and put in an array '@buddy_array' 191 # In this case the buddies of the account found earlier are retrieved and put in an array '@buddy_array'
190 # Further down an accessor method is used, 'get_name()' -- see source for details on the full set of methods 192 # Further down an accessor method is used, 'get_name()' -- see source for details on the full set of methods
191 print "Testing: Gaim::Find::buddies...\n"; 193 Gaim::Debug::info("testplugin", "Testing: Gaim::Find::buddies...\n");
192 @buddy_array = Gaim::Find::buddies($account, "USERNAME"); 194 @buddy_array = Gaim::Find::buddies($account, undef);
193 if (@buddy_array) { 195 if (@buddy_array) {
194 print "Buddies in list (" . @buddy_array . "): \n"; 196 Gaim::Debug::info("testplugin", "Buddies in list (" . @buddy_array . "): \n");
195 foreach $bud (@buddy_array) { 197 foreach $bud (@buddy_array) {
196 print Gaim::Buddy::get_name($bud) . "\n"; 198 Gaim::Debug::info("testplugin", Gaim::BuddyList::Buddy::get_name($bud) . "\n");
197 } 199 }
198 } 200 }
199 } 201 }
200 @endcode 202 @endcode
201 203
202 The BuddyList API allows for plugins to edit buddies in the list, find the buddies on a given account, set alias, and manipulate the structer as needed. It is also contains the methods for accessing @c Gaim::Group and @c Gaim::Chat types. 204 The BuddyList API allows for plugins to edit buddies in the list, find the buddies on a given account, set alias, and manipulate the structure as needed. It is also contains the methods for accessing @c Gaim::BuddyList::Group and @c Gaim::BuddyList::Chat types.
203 205
204 @section conn-api Connection API 206 @section conn-api Connection API
205 207
206 The @c Gaim::Connection API is one of the many packages that will not be covered in depth in this tutorial. They are more useful to protocol plugin developers. However, the entire @c gaim_connection_ API has corresponding, functioning perl subroutines. 208 The @c Gaim::Connection API is one of the many packages that will not be covered in depth in this tutorial. They are more useful to protocol plugin developers. However, the entire @c gaim_connection_ API has corresponding, functioning perl subroutines.
207 209
208 @section conv-api Conversation API 210 @section conv-api Conversation API
209 211
210 The Gaim perl API for @c gaim_conversation_ and @c gaim_conv_window_ allow 212 The Gaim perl API for @c gaim_conversation_ and @c gaim_conv_window_ allow
211 plugins to interact with open conversations, create new conversations, and 213 plugins to interact with open conversations, create new conversations, and
212 modify conversations at will. The following example again replaces the @c 214 modify conversations at will. In the example script, a new window is created,
213 plugin_load subroutine. In the example script, a new window is created, 215 displayed and a new conversation instant message is created. The following
214 displayed and a new conversation instant message is created. The @c 216 example again replaces the @c plugin_load subroutine. The
215 Gaim::Conversation::Chat package handles the @c gaim_conv_chat_ portion of the 217 @c Gaim::Conversation::Chat package handles the @c gaim_conv_chat_ portion of
216 API very similarly to the examples that follow. 218 the API very similarly to the examples that follow.
219
220 Notice that the interaction with the conversation window is in the @c Gaim::GtkUI package as it
221 is UI-specific code interacting with gtkgaim and not libgaim.
222 To use any of the Gaim::GtkUI functionality, you will need to add the following to the top of your script: @code use Gaim::GtkUI; @endcode
223
217 224
218 @code 225 @code
219 sub plugin_load { 226 sub plugin_load {
220 my $plugin = shift; 227 my $plugin = shift;
221 my $protocol = "prpl-oscar"; 228 my $protocol = "prpl-oscar";
222 229 my $account_name = "test";
223 $account = Gaim::Accounts::find("USERNAME", $protocol); 230
231 $account = Gaim::Accounts::find($account_name, $protocol);
224 232
225 # First we create two new conversations. 233 # First we create two new conversations.
226 print "Testing Gaim::Conversation::new()..."; 234 print "Testing Gaim::Conversation->new()...";
227 $conv1 = Gaim::Conversation::new(1, $account, "Test Conversation 1"); 235 $conv1 = Gaim::Conversation->new(1, $account, "Test Conversation 1");
228 if ($conv1) { print "ok.\n"; } else { print "fail.\n"; } 236 if ($conv1) { print "ok.\n"; } else { print "fail.\n"; }
229 237
230 print "Testing Gaim::Conversation::new()..."; 238 print "Testing Gaim::Conversation->new()...";
231 $conv2 = Gaim::Conversation::new(1, $account, "Test Conversation 2"); 239 $conv2 = Gaim::Conversation->new(1, $account, "Test Conversation 2");
232 if ($conv2) { print "ok.\n"; } else { print "fail.\n"; } 240 if ($conv2) { print "ok.\n"; } else { print "fail.\n"; }
233 241
234 # Second we create a window to display the conversations in. 242 # Second we create a window to display the conversations in.
235 # Note that the package here is Gaim::Conversation::Window 243 # Note that the package here is Gaim::GtkUI::Conversation::Window
236 print "Testing Gaim::Conversation::Window::new()...\n"; 244 print "Testing Gaim::GtkUI::Conversation::Window->new()...\n";
237 $win = Gaim::Conversation::Window::new(); 245 $win = Gaim::GtkUI::Conversation::Window->new();
238 246
239 # The third thing to do is to add the two conversations to the windows. 247 # The third thing to do is to move second the conversation to a new window.
240 # The subroutine add_conversation() returns the number of conversations 248 # The subroutine add_gtkconv() returns the number of conversations
241 # present in the window. 249 # present in the window.
242 print "Testing Gaim::Conversation::Window::add_conversation()..."; 250 print "Testing Gaim::GtkUI::Conversation::Window::add_conversation()...";
243 $conv_count = $conv1->add_conversation(); 251 $gtkconv2 = Gaim::GtkUI::Conversation::get_gtkconv($conv2);
244 if ($conv_count) { 252 $gtkconv2->get_window()->remove_gtkconv($gtkconv2);
245 print "ok..." . $conv_count . " conversations...\n"; 253 $conv_count = $win->add_gtkconv($gtkconv2);
246 } else {
247 print "fail.\n";
248 }
249
250 print "Testing Gaim::Conversation::Window::add_conversation()...";
251 $conv_count = $win->add_conversation($conv2);
252 if ($conv_count) { 254 if ($conv_count) {
253 print "ok..." . $conv_count . " conversations...\n"; 255 print "ok..." . $conv_count . " conversations...\n";
254 } else { 256 } else {
255 print "fail.\n"; 257 print "fail.\n";
256 } 258 }
257 259
258 # Now the window is displayed to the user. 260 # Now the window is displayed to the user.
259 print "Testing Gaim::Conversation::Window::show()...\n"; 261 print "Testing Gaim::GtkUI::Conversation::Window::show()...\n";
260 $win->show(); 262 $win->show();
261 263
262 # Use get_im_data() to get a handle for the conversation 264 # Use get_im_data() to get a handle for the conversation
263 print "Testing Gaim::Conversation::get_im_data()...\n"; 265 print "Testing Gaim::Conversation::get_im_data()...\n";
264 $im = $conv1->get_im_data(); 266 $im = $conv1->get_im_data();
265 if ($im) { print "ok.\n"; } else { print "fail.\n"; } 267 if ($im) { print "ok.\n"; } else { print "fail.\n"; }
266 268
267 # Here we send messages to the conversation 269 # Here we send messages to the conversation
268 print "Testing Gaim::Conversation::IM::send()...\n"; 270 print "Testing Gaim::Conversation::IM::send()...\n";
269 $im->send("Message Test."); 271 $im->send("Message Test.");
270 272
271 print "Testing Gaim::Conversation::IM::write()...\n"; 273 print "Testing Gaim::Conversation::IM::write()...\n";
272 $im->write("SENDER", "<b>Message</b> Test.", 0, 0); 274 $conv2->get_im_data()->write("SENDER", "<b>Message</b> Test.", 0, 0);
273 } 275 }
274 @endcode 276 @endcode
275 277
276 The next block of code shows how a script can close a known conversation window 278 The next block of code shows how a script can close a known conversation window
277 @c $win. 279 @c $win.
278 280
279 @code 281 @code
280 print "Testing Gaim::Conversation::Window::get_conversation_count()...\n"; 282 print "Testing Gaim::GtkUI::Conversation::Window::get_gtkconv_count()...\n";
281 $conv_count = $win->get_conversation_count(); 283 $conv_count = $win->get_gtkconv_count();
282 print "...and it returned $conv_count.\n"; 284 print "...and it returned $conv_count.\n";
283 if ($conv_count > 0) { 285 if ($conv_count > 0) {
284 print "Testing Gaim::Conversation::Window::destroy()...\n"; 286 print "Testing Gaim::GtkUI::Conversation::Window::destroy()...\n";
285 $win->destroy(); 287 $win->destroy();
286 } 288 }
287 @endcode 289 @endcode
288 290
289 @section plugin-pref-api Plugin Preference and Gtk Preference API 291 @section plugin-pref-api Plugin Preference and Gtk Preference API
290 292
301 303
302 The perl subroutine @c prefs_info_cb will be called to create the tab for the perl plugin in the Preferences dialog. An example of this function will explain the details of creating a preference frame. However, it is necessary to first create the preferences from @c plugin_load as follows. 304 The perl subroutine @c prefs_info_cb will be called to create the tab for the perl plugin in the Preferences dialog. An example of this function will explain the details of creating a preference frame. However, it is necessary to first create the preferences from @c plugin_load as follows.
303 305
304 @code 306 @code
305 sub plugin_load { 307 sub plugin_load {
306 my $plugin = shift; 308 my $plugin = shift;
307 309
308 # Here we are adding a set of preferences 310 # Here we are adding a set of preferences
309 # The second argument is the default value for the preference. 311 # The second argument is the default value for the preference.
310 Gaim::Prefs::add_none("/plugins/core/perl_test"); 312 Gaim::Prefs::add_none("/plugins/core/perl_test");
311 Gaim::Prefs::add_bool("/plugins/core/perl_test/bool", 1); 313 Gaim::Prefs::add_bool("/plugins/core/perl_test/bool", 1);
312 Gaim::Prefs::add_string("/plugins/core/perl_test/choice", "ch1"); 314 Gaim::Prefs::add_string("/plugins/core/perl_test/choice_str", "ch1");
313 Gaim::Prefs::add_string("/plugins/core/perl_test/text", "Foobar"); 315 Gaim::Prefs::add_int("/plugins/core/perl_test/choice_int", 1);
316 Gaim::Prefs::add_string("/plugins/core/perl_test/text", "Foobar");
314 } 317 }
315 @endcode 318 @endcode
316 319
317 Now we can add these preferences from inside our function specified in @c \%PLUGIN_INFO . 320 Now we can add these preferences from inside our function specified in @c \%PLUGIN_INFO .
318 321
319 @code 322 @code
320 sub prefs_info_cb { 323 sub prefs_info_cb {
321 # The first step is to initialize the Gaim::Pref::Frame that will be returned 324 # The first step is to initialize the Gaim::Pref::Frame that will be returned
322 $frame = Gaim::PluginPref::Frame->new(); 325 $frame = Gaim::PluginPref::Frame->new();
323 326
324 # Create a new boolean option with a label "Boolean Label" and then add 327 # Create a new boolean option with a label "Boolean Label" and then add
325 # it to the frame 328 # it to the frame
326 $ppref = Gaim::Pref->new_with_label("Boolean Label"); 329 $ppref = Gaim::PluginPref->new_with_label("Boolean Label");
327 $frame->add($ppref); 330 $frame->add($ppref);
328 331
329 $ppref = Gaim::PluginPref->new_with_name_and_label( 332 $ppref = Gaim::PluginPref->new_with_name_and_label(
330 "/plugins/core/perl_test/bool", "Boolean Preference"); 333 "/plugins/core/perl_test/bool", "Boolean Preference");
331 $frame->add($ppref); 334 $frame->add($ppref);
332 335
333 # Create a set of choices. To do so we must set the type to 1 which is 336 # Create a set of choices. To do so we must set the type to 1 which is
334 # the numerical equivelant of the GaimPrefType for choice. 337 # the numerical equivalent of the GaimPrefType for choice.
335 $ppref = Gaim::PluginPref->new_with_name_and_label( 338 $ppref = Gaim::PluginPref->new_with_name_and_label(
336 "/plugins/core/perl_test/choice", "Choice Preference"); 339 "/plugins/core/perl_test/choice_str", "Choice Preference");
337 $ppref->set_type(1); 340 $ppref->set_type(1);
338 $ppref->add_choice("ch0", $frame); 341 $ppref->add_choice("ch0", "ch0");
339 # The following will be the default value as set from plugin_load 342 # The following will be the default value as set from plugin_load
340 $ppref->add_choice("ch1", $frame); 343 $ppref->add_choice("ch1", "ch1");
341 $frame->add($ppref); 344 $frame->add($ppref);
345
346 # Create a set of choices. To do so we must set the type to 1 which is
347 # the numerical equivalent of the GaimPrefType for choice.
348 $ppref = Gaim::PluginPref->new_with_name_and_label(
349 "/plugins/core/perl_test/choice_int", "Choice Preference 2");
350 $ppref->set_type(1);
351 $ppref->add_choice("zero", 0);
352 # The following will be the default value as set from plugin_load
353 $ppref->add_choice("one", 1);
354 $frame->add($ppref);
355
342 356
343 # Create a text box. The default value will be "Foobar" as set by 357 # Create a text box. The default value will be "Foobar" as set by
344 # plugin_load 358 # plugin_load
345 $ppref = Gaim::PluginPref->new_with_name_and_label( 359 $ppref = Gaim::PluginPref->new_with_name_and_label(
346 "/plugins/core/perl_test/text", "Text Box Preference"); 360 "/plugins/core/perl_test/text", "Text Box Preference");
361 $ppref->set_type(2);
347 $ppref->set_max_length(16); 362 $ppref->set_max_length(16);
348 $frame->add($ppref); 363 $frame->add($ppref);
349 364
350 return $frame; 365 return $frame;
351 } 366 }
352 @endcode 367 @endcode
353 368
369 <!--
354 Using the Gtk2-Perl module for Perl it is possible to create tailored @c GtkFrame elements and display them in a preference window. Note that Gtk2-Perl must be loaded with @c require and not @c use . The first step is to create the proper key/value pairs in the @c \%PLUGIN_INFO hash noting that the @c prefs_info key is no longer valid. Instead the keys @c GTK_UI and @c gtk_prefs_info must be set as follows. 370 Using the Gtk2-Perl module for Perl it is possible to create tailored @c GtkFrame elements and display them in a preference window. Note that Gtk2-Perl must be loaded with @c require and not @c use . The first step is to create the proper key/value pairs in the @c \%PLUGIN_INFO hash noting that the @c prefs_info key is no longer valid. Instead the keys @c GTK_UI and @c gtk_prefs_info must be set as follows.
355 371
356 @code 372 @code
357 %PLUGIN_INFO = { 373 %PLUGIN_INFO = {
358 ..., 374 ...,
365 To finish this example @c gtk_prefs_info_cb needs to be defined. To introduce some of the flexibility of using Gtk2-Perl the example also includes a button and a callback for the button. Explaining Gtk2-Perl is beyond the scope of this tutorial and more info can be found at the project's website <a href="http://gtk2-perl.sourceforge.net/">http://gtk2-perl.sourceforge.net/</a>. 381 To finish this example @c gtk_prefs_info_cb needs to be defined. To introduce some of the flexibility of using Gtk2-Perl the example also includes a button and a callback for the button. Explaining Gtk2-Perl is beyond the scope of this tutorial and more info can be found at the project's website <a href="http://gtk2-perl.sourceforge.net/">http://gtk2-perl.sourceforge.net/</a>.
366 382
367 @code 383 @code
368 # A simple call back that prints out whatever value it is given as an argument. 384 # A simple call back that prints out whatever value it is given as an argument.
369 sub button_cb { 385 sub button_cb {
370 my $widget = shift; 386 my $widget = shift;
371 my $data = shift; 387 my $data = shift;
372 print "Clicked button with message: " . $data . "\n"; 388 print "Clicked button with message: " . $data . "\n";
373 } 389 }
374 390
375 sub gtk_prefs_info_cb { 391 sub gtk_prefs_info_cb {
376 # Create a button that prints a message to the console and places it in the frame. 392 # Create a button that prints a message to the console and places it in the frame.
377 use Glib; 393 use Glib;
378 require Gtk2; 394 require Gtk2;
379 395
380 $frame = Gtk2::Frame->new(\'Gtk Test Frame\'); 396 $frame = Gtk2::Frame->new(\'Gtk Test Frame\');
381 $button = Gtk2::Button->new(\'Print Message\'); 397 $button = Gtk2::Button->new(\'Print Message\');
382 398
383 $frame->set_border_width(10); 399 $frame->set_border_width(10);
384 $button->set_border_width(150); 400 $button->set_border_width(150);
385 $button->signal_connect("clicked" => \&button_cb, "Message Text"); 401 $button->signal_connect("clicked" => \&button_cb, "Message Text");
386 $frame->add($button); 402 $frame->add($button);
387 403
388 $button->show(); 404 $button->show();
389 $frame->show(); 405 $frame->show();
390 406
391 return $frame; 407 return $frame;
392 } 408 }
393 @endcode 409 @endcode
410 -->
394 411
395 @section request-api Request Dialog Box API 412 @section request-api Request Dialog Box API
396 413
397 The @c Gaim::Request package allows for plugins to have interactive dialog boxes without the need for creating them in Gtk creating a seperation between the user interfaace and the plugin. The portion of the Request API available to perl scripts is listed below followed by an example illustrating their use. 414 The @c Gaim::Request package allows for plugins to have interactive dialog boxes without the need for creating them in Gtk creating a seperation between the user interfaace and the plugin. The portion of the Request API available to perl scripts is listed below followed by an example illustrating their use.
398 415
399 These arguments are the same for each of the three request types: 416 These arguments are the same for each of the three request types:
400 @arg @em handle - The plugin handle. 417 @arg @em handle - The plugin handle.
401 @arg @em title - String title for the dialog. 418 @arg @em title - String title for the dialog.
402 @arg @em primary - The first sub-heading. 419 @arg @em primary - The first sub-heading.
403 @arg @em secondary - The second sub-heading. 420 @arg @em secondary - The second sub-heading.
404 @arg @em ok_text - The Text for the OK button. 421 @arg @em ok_text - The Text for the OK button.
405 @arg @em ok_cb - The string name of the perl subroutine to call when the OK button is clicked. 422 @arg @em ok_cb - The string name of the perl subroutine to call when the OK button is clicked.
406 @arg @em cancel_text - The text for the Cancel button. 423 @arg @em cancel_text - The text for the Cancel button.
407 @arg @em cancel_cb - The string name of the perl subroutine to call when the Cancel button is clicked. 424 @arg @em cancel_cb - The string name of the perl subroutine to call when the Cancel button is clicked.
408 @arg @em default_value - Default text string to display in the input box. 425 @arg @em default_value - Default text string to display in the input box.
409 @arg @em multiline - Boolean where true indicates multiple line input boxes are allowed. 426 @arg @em multiline - Boolean where true indicates multiple line input boxes are allowed.
410 @arg @em masked - Boolean indicating if the user can edit the text. 427 @arg @em masked - Boolean indicating if the user can edit the text.
411 @arg @em hint - See source for more information - can be left blank. 428 @arg @em hint - See source for more information - can be left blank.
412 @arg @em filename - String defualt file name value. 429 @arg @em filename - String defualt file name value.
413 @arg @em savedialog - Boolean where true indicates use as a save file dialog and false indicates an open file dialog. 430 @arg @em savedialog - Boolean where true indicates use as a save file dialog and false indicates an open file dialog.
414 431
415 @code 432 @code
416 # Create a simple text input box 433 # Create a simple text input box
417 Gaim::Request::input(handle, title, primary, secondary, default_value, multiline, masked, hint, ok_text, ok_cb, cancel_text, cancel_cb); 434 Gaim::Request::input(handle, title, primary, secondary, default_value, multiline, masked, hint, ok_text, ok_cb, cancel_text, cancel_cb);
418 435
419 # Propt user to select a file 436 # Prompt user to select a file
420 Gaim::Request::file(handle, title, filename, savedialog, ok_cb, cancel_cb); 437 Gaim::Request::file(handle, title, filename, savedialog, ok_cb, cancel_cb);
421 438
422 # Create a unique input dialog as shown in the following example 439 # Create a unique input dialog as shown in the following example
423 Gaim::Request::fields(handle, title, primary, secondary, fields, ok_text, ok_cb, cancel_text, cancel_cb); 440 Gaim::Request::fields(handle, title, primary, secondary, fields, ok_text, ok_cb, cancel_text, cancel_cb);
424 @endcode 441 @endcode
427 444
428 @code 445 @code
429 sub ok_cb_test{ 446 sub ok_cb_test{
430 # The $fields is passed to the callback function when the button is clicked. 447 # The $fields is passed to the callback function when the button is clicked.
431 # To get the values they must be extracted from $fields by name. 448 # To get the values they must be extracted from $fields by name.
432 $fields = shift; 449 $fields = shift;
433 $account = Gaim::Request::fields_get_account($fields, "acct_test"); 450 $account = Gaim::Request::Fields::get_account($fields, "acct_test");
434 $int = Gaim::Request::fields_get_integer($fields, "int_test"); 451 $int = Gaim::Request::Fields::get_integer($fields, "int_test");
435 $choice = Gaim::Request::fields_get_choice($fields, "ch_test"); 452 $choice = Gaim::Request::Fields::get_choice($fields, "ch_test");
436 } 453 }
437 454
438 sub cancel_cb_test{ 455 sub cancel_cb_test{
439 # Cancel does nothing but is symmetric to the ok_cb_test 456 # Cancel does nothing but is symmetric to the ok_cb_test
440 } 457 }
441 458
442 sub plugin_load { 459 sub plugin_load {
443 my $plugin = shift; 460 my $plugin = shift;
444 461
445 # Create a group to pool together mutltiple fields. 462 # Create a group to pool together mutltiple fields.
446 $group = Gaim::Request::field_group_new("Group Name"); 463 $group = Gaim::Request::Field::Group::new("Group Name");
447 464
448 # Each fields is created with Gaim::Request::*_new(), is made viewable with Gaim::Request::field_*_set_show_all() 465 # Each fields is created with Gaim::Request::*_new(), is made viewable with Gaim::Request::field_*_set_show_all()
449 # and is then added to the group with Gaim::Request::field_group_add_field() 466 # and is then added to the group with Gaim::Request::field_group_add_field()
450 467
451 # Add an account drop down list showing all active accounts 468 # Add an account drop down list showing all active accounts
452 $field = Gaim::Request::field_account_new("acct_test", "Account Text", undef); 469 $field = Gaim::Request::Field::account_new("acct_test", "Account Text", undef);
453 Gaim::Request::field_account_set_show_all($field, 0); 470 Gaim::Request::Field::account_set_show_all($field, 0);
454 Gaim::Request::field_group_add_field($group, $field); 471 Gaim::Request::Field::Group::add_field($group, $field);
455 472
456 # Add an integer input box 473 # Add an integer input box
457 $field = Gaim::Request::field_int_new("int_test", "Integer Text", 33); 474 $field = Gaim::Request::Field::int_new("int_test", "Integer Text", 33);
458 Gaim::Request::field_group_add_field($group, $field); 475 Gaim::Request::Field::Group::add_field($group, $field);
459 476
460 # Add a list of choices 477 # Add a list of choices
461 $field = Gaim::Request::field_choice_new("ch_test", "Choice Text", 1); 478 $field = Gaim::Request::Field::choice_new("ch_test", "Choice Text", 1);
462 Gaim::Request::field_choice_add($field, "Choice 0"); 479 Gaim::Request::Field::choice_add($field, "Choice 0");
463 Gaim::Request::field_choice_add($field, "Choice 1"); 480 Gaim::Request::Field::choice_add($field, "Choice 1");
464 Gaim::Request::field_choice_add($field, "Choice 2"); 481 Gaim::Request::Field::choice_add($field, "Choice 2");
465 482
466 Gaim::Request::field_group_add_field($group, $field); 483 Gaim::Request::Field::Group::add_field($group, $field);
467 484
468 # Create a Gaim::Request and add the group that was just created. 485 # Create a Gaim::Request and add the group that was just created.
469 $request = Gaim::Request::fields_new(); 486 $request = Gaim::Request::Fields::new();
470 Gaim::Request::fields_add_group($request, $group); 487 Gaim::Request::Fields::add_group($request, $group);
471 488
472 # Display the dialog box with the input fields added earlier with the appropriate titles. 489 # Display the dialog box with the input fields added earlier with the appropriate titles.
473 Gaim::Request::fields( 490 Gaim::Request::fields(
474 $plugin, 491 $plugin,
475 "Request Title!", 492 "Request Title!",
476 "Primary Title", 493 "Primary Title",
477 "Secondary Title", 494 "Secondary Title",
478 $request, 495 $request,
479 "Ok Text", "ok_cb_test", 496 "Ok Text", "ok_cb_test",
480 "Cancel Text", "cancel_cb_test"); 497 "Cancel Text", "cancel_cb_test");
481 } 498 }
482 @endcode 499 @endcode
483 500
484 @section timeout-cb Misc: Plugin Actions, Timeouts and Callbacks 501 @section timeout-cb Misc: Plugin Actions, Timeouts and Callbacks
485 502
503 # The callback subroutine that is called when "Tools -> Plugin -> Action 2" is selected 520 # The callback subroutine that is called when "Tools -> Plugin -> Action 2" is selected
504 "Action 2" => \&action2_cb 521 "Action 2" => \&action2_cb
505 ); 522 );
506 523
507 sub action1_cb { 524 sub action1_cb {
508 Gaim::debug_info("Test Plugin", "Action 1 activated"); 525 Gaim::Debug::info("Test Plugin", "Action 1 activated\n");
509 } 526 }
510 527
511 sub action2_cb { 528 sub action2_cb {
512 Gaim::debug_info("Test Plugin", "Action 2 activated"); 529 Gaim::Debug::info("Test Plugin", "Action 2 activated\n");
513 } 530 }
514 @endcode 531 @endcode
515 532
516 Timeouts allow a perl subroutine to be exectued after a specified time. They only occur once, so as stated earlier the timeout must be reregistered after every time it is called. 533 Timeouts allow a perl subroutine to be exectued after a specified time. They only occur once, so as stated earlier the timeout must be reregistered after every time it is called.
517 534
518 @code 535 @code
519 sub timeout_cb { 536 sub timeout_cb {
520 my $plugin = shift; 537 my $plugin = shift;
521 print "Timeout occurred."; 538 Gaim::Debug::info("testplugin", "Timeout occurred.\n");
522 539
523 # Reschedule timeout 540 # Reschedule timeout
524 Gaim::timeout_add($plugin, 10, \&timeout_cb, $plugin); 541 Gaim::timeout_add($plugin, 10, \&timeout_cb, $plugin);
525 } 542 }
526 543
527 sub plugin_load { 544 sub plugin_load {
528 $plugin = shift; 545 $plugin = shift;
529 546
530 # Schedule a timeout for ten seconds from now 547 # Schedule a timeout for ten seconds from now
531 Gaim::timeout_add($plugin, 10, \&timeout_cb, $plugin); 548 Gaim::timeout_add($plugin, 10, \&timeout_cb, $plugin);
532 } 549 }
533 @endcode 550 @endcode
534 551
535 Callbacks are handled by creating a perl subroutine to serve as the callback and then attaching the callback to a signal. To use callbacks it is necessary to first obtain the plugin handle with the @c Gaim::Plugin::get_handle() subroutine to pass as an argument for the callback. 552 Callbacks are handled by creating a perl subroutine to serve as the callback and then attaching the callback to a signal. To use callbacks it is necessary to first obtain the plugin handle with the @c Gaim::Plugin::get_handle() subroutine to pass as an argument for the callback.
536 553
537 @code 554 @code
538 sub signal_cb { 555 sub signal_cb {
539 # The handle and the user data come in as arguments 556 # The signal data and the user data come in as arguments
540 my ($handle, $data) = @_; 557 my ($account, $data) = @_;
541 print "User just connected."; 558 Gaim::Debug::info("testplugin", "Account \"" . $account->get_username() . "\" just connected.\n");
542 } 559 }
543 560
544 sub plugin_load { 561 sub plugin_load {
545 $plugin = shift; 562 $plugin = shift;
546 563
547 # User data to be given as an argument to the callback perl subroutine. 564 # User data to be given as an argument to the callback perl subroutine.
548 $data = ""; 565 $data = "";
549 566
550 # A pointer to the actual plugin handle needed by the callback function 567 # A pointer to the handle to which the signal belongs needed by the callback function
551 $plugin_handle = Gaim::Accounts::get_handle(); 568 $accounts_handle = Gaim::Accounts::get_handle();
552 569
553 # Connect the perl subroutine 'signal_cb' to the event 'account-connecting' 570 # Connect the perl subroutine 'signal_cb' to the event 'account-connecting'
554 Gaim::signal_connect($plugin_handle, "account-connecting", $plugin, \&signal_cb, $data); 571 Gaim::Signal::connect($accounts_handle, "account-connecting", $plugin, \&signal_cb, $data);
555 } 572 }
556 @endcode 573 @endcode
557 574
558 @section Resources 575 @section Resources
559 @see API Documentation 576 @see API Documentation