comparison doc/PERL-HOWTO.dox @ 12364:6fd82071a7b8

[gaim-migrate @ 14668] sf patch #1373688, from Will Thompson "Make the Perl bindings more Perl-ish" committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Mon, 05 Dec 2005 23:54:34 +0000
parents 11d14efe7be2
children 967dd29cc4ae
comparison
equal deleted inserted replaced
12363:f758af0373cb 12364:6fd82071a7b8
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->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.
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 and both are nearly identical to their C counterparts @c gaim_account_ and @c gaim_accounts_. The Account Option API is in the package @c Gaim::Account::Option and is identical to its C implementation @c gaim_account_option . 51 The Account API is in the @c Gaim::Account:: and @c Gaim::Accounts:: packages
52 52 and both are nearly identical to their C counterparts @c gaim_account_ and @c
53 The Account* APIs allow for scripts to create, remove, and edit accounts of the type GaimAccount. (Note: Gaim type have no real meaning in perl scripts other than the types of the arguments of the perl subroutines need to be of the expected type.) This section will not go into detail about the @c Gaim::Account::Option package for its use in building protocol plugins which are outside the scope of this document. However, most of the API calls for the @c Gaim::Account::Option package should function as expected so if there is a need to access any of the Account Options the function calls necessary are avaialbe in the Gaim perl API. 53 gaim_accounts_. The Account Option API is in the package @c
54 54 Gaim::Account::Option and is identical to its C implementation @c
55 To reduce redundant code the following code examples are going to use the template shown in the previous section. To highlight some of the more useful features of the Account API we will be replacing the @c plugin_load perl subroutine. For testing purposes we will display output on the command line by using perl @c print commands. 55 gaim_account_option.
56
57 The Account* APIs allow for scripts to create, remove, and edit accounts of the
58 type GaimAccount. (Note: Gaim types have no real meaning in perl scripts other
59 than the types of the arguments of the perl subroutines need to be of the
60 expected type.) This section will not go into detail about the @c
61 Gaim::Account::Option package for its use in building protocol plugins which
62 are outside the scope of this document. However, most of the API calls for the
63 @c Gaim::Account::Option package should function as expected so if there is a
64 need to access any of the Account Options the function calls necessary are
65 avaialbe in the Gaim perl API.
66
67 To reduce redundant code the following code examples are going to use the
68 template shown in the previous section. To highlight some of the more useful
69 features of the Account API we will be replacing the @c plugin_load perl
70 subroutine. For testing purposes we will display output on the command line by
71 using perl @c print commands.
56 72
57 @code 73 @code
58 sub plugin_load { 74 sub plugin_load {
59 $plugin = shift; 75 $plugin = shift;
60 76
61 # 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
62 my $protocol = "prpl-oscar"; 78 my $protocol = "prpl-oscar";
63 79
64 # Create a new Account 80 # Create a new Account
65 print "Testing: Gaim::Account::new()..."; 81 print "Testing: Gaim::Account::new()... ";
66 $account = Gaim::Account::new("TEST_NAME", $protocol); 82 $account = Gaim::Account->new($TEST_NAME, $PROTOCOL_ID);
67 if ($account) { print "ok.\n"; } else { print "fail.\n"; } 83 if ($account) { print "ok.\n"; } else { print "fail.\n"; }
68 84
69 # Add a new Account 85 # Add a new Account
70 print "Testing: Gaim::Account::add()..."; 86 print "Testing: Gaim::Account::add()...";
71 Gaim::Accounts::add($account); 87 Gaim::Accounts::add($account);
72 print "pending find...\n"; 88 print "pending find...\n";
75 print "Testing: Gaim::Accounts::find()..."; 91 print "Testing: Gaim::Accounts::find()...";
76 $account = Gaim::Accounts::find("TEST_NAME", $protocol); 92 $account = Gaim::Accounts::find("TEST_NAME", $protocol);
77 if ($account) { print "ok.\n"; } else { print "fail.\n"; } 93 if ($account) { print "ok.\n"; } else { print "fail.\n"; }
78 94
79 # Return the username 95 # Return the username
80 print "Testing: Gaim::Account::get_username()..."; 96 print "Testing: Gaim::Account::get_username()... ";
81 $user_name = Gaim::Account::get_username($account); 97 $user_name = $account->get_username();
82 if ($user_name) { print $user_name . "...ok.\n"; } else { print "fail.\n"; } 98 if ($user_name) {
83 99 print "Success: $user_name.\n";
100 } else {
101 print "Failed!\n";
102 }
84 # Verify if the user is connected 103 # Verify if the user is connected
85 print "Testing: Gaim::Account::is_connected()"; 104 print "Testing: Gaim::Account::is_connected()";
86 $user_connected = Gaim::Account::is_connected($account); 105 if ($account->is_connected()) {
87 if (!($user_connected)) { print "...not connected...ok..\n"; } else { print "...connected...ok.\n"; } 106 print " Connected.\n";
88 107 } else {
89 # The status mechanism is how users are Connected, set Away, Disconnected (status set to Offline), etc 108 print " Disconnected.\n";
109 }
110
111 # The status mechanism is how users are Connected, set Away,
112 # Disconnected (status set to Offline), etc
90 # $status is now a Gaim::Status perl type. 113 # $status is now a Gaim::Status perl type.
91 print "Testing: Gaim::Accounts::get_active_status()..."; 114 print "Testing: Gaim::Accounts::get_active_status()... ";
92 $status = Gaim::Account::get_active_status($account); 115 if ($account->get_active_status()) {
93 if ($status) { print "ok.\n"; } else { print "fail.\n"; } 116 print "Okay.\n";
94 117 } else {
95 # It follows that to connect a user you mest set the account status to "available" 118 print "Failed!\n";
96 # similarly we can disconnect a user by setting the account status to "offline" 119 }
120
121 # 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 # status to "offline"
124
97 $account = Gaim::Accounts::find("TEST_NAME", $protocol); 125 $account = Gaim::Accounts::find("TEST_NAME", $protocol);
98 print "Testing: Gaim::Accounts::connect()...pending...\n"; 126 print "Testing: Gaim::Accounts::connect()...pending...\n";
99 127
100 Gaim::Account::set_status($account, "available", TRUE); 128 $account->set_status("available", TRUE);
101 Gaim::Account::connect($account); 129 $account->connect();
102 } 130
103 @endcode 131 }
104 132 @endcode
105 For the most part the code listed above is explained by the comments, however there are a few other points to make. The variables above are all specialized Perl types that contain pointers to the actual Gaim types. They can be reasigned at will just like any other variable in Perl. The only way to edit the values of a Gaim type from within perl are through accessor methods such as @c Gaim::Account::get_username(). For arguments that you would make @c NULL in C should be set to @c undef in Perl. 133
134 For the most part the code listed above is explained by the comments, however
135 there are a few other points to make. The variables above are all specialized
136 Perl types that contain pointers to the actual Gaim types. They can be
137 reasigned at will just like any other variable in Perl. The only way to edit
138 the values of a Gaim type from within perl are through accessor methods such as
139 @c Gaim::Account::get_username(). For arguments that you would make @c NULL in
140 C should be set to @c undef in Perl.
106 141
107 @section buddylist-api Buddylist, Group and Chat API 142 @section buddylist-api Buddylist, Group and Chat API
108 143
109 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. 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.
110 145
282 Now we can add these preferences from inside our function specified in @c \%PLUGIN_INFO . 317 Now we can add these preferences from inside our function specified in @c \%PLUGIN_INFO .
283 318
284 @code 319 @code
285 sub prefs_info_cb { 320 sub prefs_info_cb {
286 # The first step is to initialize the Gaim::Pref::Frame that will be returned 321 # The first step is to initialize the Gaim::Pref::Frame that will be returned
287 $frame = Gaim::Pref::frame_new(); 322 $frame = Gaim::PluginPref::Frame->new();
288 323
289 # Create a new boolean option with a label "Boolean Label" and then add it to the frame 324 # Create a new boolean option with a label "Boolean Label" and then add
290 $ppref = Gaim::Pref::new_with_label("Boolean Label"); 325 # it to the frame
291 Gaim::Pref::frame_add($frame, $ppref); 326 $ppref = Gaim::Pref->new_with_label("Boolean Label");
292 327 $frame->add($ppref);
293 $ppref = Gaim::Pref::new_with_name_and_label("/plugins/core/perl_test/bool", "Boolean Preference"); 328
294 Gaim::Pref::frame_add($frame, $ppref); 329 $ppref = Gaim::PluginPref->new_with_name_and_label(
295 330 "/plugins/core/perl_test/bool", "Boolean Preference");
296 # Create a set of choices. To do so we must set the type to 1 which is the numerical equivelant of 331 $frame->add($ppref);
297 # the GaimPrefType for choice. 332
298 $ppref = Gaim::Pref::new_with_name_and_label("/plugins/core/perl_test/choice", "Choice Preference"); 333 # Create a set of choices. To do so we must set the type to 1 which is
299 Gaim::Pref::set_type($ppref, 1); 334 # the numerical equivelant of the GaimPrefType for choice.
300 Gaim::Pref::add_choice($ppref, "ch0", $frame); 335 $ppref = Gaim::PluginPref->new_with_name_and_label(
336 "/plugins/core/perl_test/choice", "Choice Preference");
337 $ppref->set_type(1);
338 $ppref->add_choice("ch0", $frame);
301 # The following will be the default value as set from plugin_load 339 # The following will be the default value as set from plugin_load
302 Gaim::Pref::add_choice($ppref, "ch1", $frame); 340 $ppref->add_choice("ch1", $frame);
303 Gaim::Pref::frame_add($frame, $ppref); 341 $frame->add($ppref);
304 342
305 # Create a text box. The default value will be "Foobar" as set by plugin_load 343 # Create a text box. The default value will be "Foobar" as set by
306 $ppref = Gaim::Pref::new_with_name_and_label("/plugins/core/perl_test/text", "Text Box Preference"); 344 # plugin_load
307 Gaim::Pref::set_max_length($ppref, 16); 345 $ppref = Gaim::PluginPref->new_with_name_and_label(
308 Gaim::Pref::frame_add($frame, $ppref); 346 "/plugins/core/perl_test/text", "Text Box Preference");
347 $ppref->set_max_length(16);
348 $frame->add($ppref);
309 349
310 return $frame; 350 return $frame;
311 } 351 }
312 @endcode 352 @endcode
313 353