bfm%3a978-1-4302-0047-5%2f1
TRANSCRIPT
-
8/12/2019 bfm%3A978-1-4302-0047-5%2F1
1/19
Open Source MessagingApplication Development:
Building and Extending GaimSEAN EGAN
-
8/12/2019 bfm%3A978-1-4302-0047-5%2F1
2/19
Open Source Messaging Application Development: Building and Extending Gaim
Copyright 2005 by Sean Egan
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN (pbk): 1-59059-467-3
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Lead Editor: Jason Gilmore
Technical Reviewer: Nathan Walp
Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Tony Davis, Jason Gilmore,
Jonathan Hassell, Chris Mills, Dominic Shakeshaft, Jim Sumser
Associate Publisher: Grace Wong
Project Manager: Beth Christmas
Copy Edit Manager: Nicole LeClerc
Copy Editor: Candace English
Production Manager: Kari Brooks-CoponyProduction Editor: Kelly Winquist
Compositor: Susan Glinert and Wordstop Technologies Pvt. Ltd., Chennai
Proofreader: Linda Seifert
Indexer: Broccoli Information Services
Artist: Kinetic Publishing Services, LLC
Cover Designer: Kurt Krames
Manufacturing Manager: Tom Debolski
Distributed to the book trade in the United States by Springer-Verlag New York, Inc., 233 Spring Street, 6th
Floor, New York, NY 10013, and outside the United States by Springer-Verlag GmbH & Co. KG,
Tiergartenstr. 17, 69112 Heidelberg, Germany.
In the United States: phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected] , or visithttp://www.springer-ny.com. Outside the United States: fax +49 6221 345229, e-mail [email protected],or visit http://springer.de.
For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA
94710. Phone 510-549-5930, fax 510-549-5939, [email protected], or visit http://www.apress.com.
The information in this book is distributed on an as is basis, without warranty. Although every
precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any
liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly
or indirectly by the information contained in this work.
The source code for this book is available to readers at http://www.apress.com in the Downloads section.
-
8/12/2019 bfm%3A978-1-4302-0047-5%2F1
3/19
For Dad
-
8/12/2019 bfm%3A978-1-4302-0047-5%2F1
4/19
Contents at a Glance
About the Author. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xv
About the Technical Reviewer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Acknowledgments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
CHAPTER 1 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
CHAPTER 2 The Open Source Development Process . . . . . . . . . . . . . . . . . . . . . . 23
CHAPTER 3 Development Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
CHAPTER 4 Programming Gaim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
CHAPTER 5 GTK+ Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
CHAPTER 6 Advanced GTK+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
CHAPTER 7 Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
CHAPTER 8 Protocol Plug-Ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
CHAPTER 9 Internationalization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
CHAPTER 10 Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
INDEX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
-
8/12/2019 bfm%3A978-1-4302-0047-5%2F1
5/19
-
8/12/2019 bfm%3A978-1-4302-0047-5%2F1
6/19
viii C ONT ENT S
Contributing to Open Source Development . . . . . . . . . . . . . . . . . . . . . . . . . 31
Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Climbing the Ranks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Managing Your Own Open Source Project . . . . . . . . . . . . . . . . . . . . . . . . . 34
Becoming a Maintainer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Management Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
CHAPTER 3 Development Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Emacs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
vi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
IDEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
GCC. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
How a Program Is Built. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Invoking GCC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
make . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Makefile Rules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Implicit Rules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Multiple Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Autotools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Automake. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Autoconf. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Other Required Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
autogen.sh. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60Using the Build Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
GDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Running Your Application Within GDB . . . . . . . . . . . . . . . . . . . . . . . . 61
Analyzing Core Dumps with GDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Debugging Segfaults . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
CVS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Versioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Branches. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65Using CVS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
ViewCVS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
SourceForge.net. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Submitting a Tracker Item . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Managing Tracker Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
-
8/12/2019 bfm%3A978-1-4302-0047-5%2F1
7/19
C ONT ENT S
CHAPTER 4 Programming Gaim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Compiling Your First Plug-In: helloworld.c. . . . . . . . . . . . . . . . . . . . . . 85Anatomy of a Gaim Plug-in . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
GAIM_INIT_PLUGIN. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
GaimPluginInfo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89plugin_load(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92#includeStatements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Inheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Object-Oriented C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Inheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Accessor and Mutator Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . 100Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Hash Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
The Gaim API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Core/UI Split. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
GaimAccount (account.h) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108GaimConnection (connection.h). . . . . . . . . . . . . . . . . . . . . . 108GaimConversation (conversation.h) . . . . . . . . . . . . . . . . . 109GaimBuddyList (blist.h). . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
CHAPTER 5 GTK+ Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Overview of GTK+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Language Bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Anatomy of a GTK+ Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
The gtk_init()Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118The Main Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
User Interface Principles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Elegance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
GNOME HIG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
-
8/12/2019 bfm%3A978-1-4302-0047-5%2F1
8/19
x C ONT ENT S
Creating Dialogs in GTK+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Widget Packing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Container Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Event Listeners. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136A Sample GTK+ Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Gaim Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
GTK+ Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
GtkLabel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139GtkImage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140GtkProgressBar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141GtkEntry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143GtkSpinButton. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146GtkButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147GtkToggleButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150GtkCheckButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151GtkRadioButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152GtkTextView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153GtkTreeView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158GtkComboBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163GtkToolbar. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164GtkMenu. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167GtkDialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
GtkFileChooserDialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171A Gaim Plug-in Example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Gaim Plug-in Boilerplate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
The Data Structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Using Gaim Signals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Scanning Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Hooking into Account Actions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Creating the GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
The Entire Plug-In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Potential Enhancements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
-
8/12/2019 bfm%3A978-1-4302-0047-5%2F1
9/19
C ONT ENT S
CHAPTER 6 Advanced GTK+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
A Sample Plug-In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
GObject. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Object-Oriented Features of GObject. . . . . . . . . . . . . . . . . . . . . . . . 199
Using GObject. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202Creating a Composite Widget . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
GdkPixbuf. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Working with Image Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
GdkPixbufLoader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220Animations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Pango . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
PangoLayout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222PangoAttribute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
GDK. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
The X Window System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224GdkDrawable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226Drawing Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
OverridingGtkWidget . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232Data Fields of GtkWidget . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232Overriding Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Receiving Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
CHAPTER 7 Sockets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
The OSI Seven-Layer Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Domain Name Service. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Connecting to a Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Accepting Connections from Clients. . . . . . . . . . . . . . . . . . . . . . . . . 248
Reading and Writing Data to a Socket. . . . . . . . . . . . . . . . . . . . . . . 252
Non-blocking I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
select(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
fd_set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254timeval. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254Non-Blocking Sockets with select(). . . . . . . . . . . . . . . . . . . . . . 255
Integrating into GLibs Main Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
GIOChannel. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256g_io_add_watch(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
-
8/12/2019 bfm%3A978-1-4302-0047-5%2F1
10/19
xii C ONT ENT S
A Sample Gaim Plug-in . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
The Boilerplate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
plugin_load(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258incoming_cb(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259create_response() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
The Final Plug-In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261Possible Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
CHAPTER 8 Protocol Plug-Ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Protocol Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Packet Framing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Packet Headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Packet Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Protocol Implementation in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275Binary Protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Text Protocols. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Learning Protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Reverse Engineering. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Legal Ramifications of Reverse Engineering . . . . . . . . . . . . . . . . . . 281
Packet Sniffing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Reverse Engineering Techniques. . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Interfacing with Gaim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
server.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
GaimPluginProtocolInfo. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289Command Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Program Flow of a prpl. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
CHAPTER 9 Internationalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Internationalization Defined . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
Text Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
ASCII . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Foreign Encodings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302iconv. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Knowing What Encodings to Use . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Unicode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Code Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Encodings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
UTF-8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
-
8/12/2019 bfm%3A978-1-4302-0047-5%2F1
11/19
C ONT ENT S
Translations with gettext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
An Overview of gettext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Setting Up gettext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Coding with gettext. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Translating with gettext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Using Translations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316gettext Summarized . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
CHAPTER 10 Portability. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
The C Programming Language. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
K&R, ANSI, and C99 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
libc. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
Tips on Writing Portable C Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
POSIX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Porting to Windows with MinGW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Cygwin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Windows Compatibility Libraries. . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
GLib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Portable Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Utility Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
Memory Allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
File Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Plug-Ins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
GDK. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
X11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Win32 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
WIMP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
-
8/12/2019 bfm%3A978-1-4302-0047-5%2F1
12/19
About the Author
SEAN EGANmaintains the Gaim project, http://gaim.sourceforge.net.He is
a major contributor to the project, managing a team of 10 core developers,
and reviewing and incorporating patches from over 200 contributors. A
Long Island, NY native, Sean has a bachelors degree in computer science
from Binghamton Universtiy.
Sean has been involved in Gaim development for five years and learned
most of what he knows about software development from his experience
with the project.
-
8/12/2019 bfm%3A978-1-4302-0047-5%2F1
13/19
About the Technical Reviewer
NATHAN WALPis a software engineer at Recognition Research, Inc. in Blacksburg, VA. He has
more than five years experience writing software, and has been working on Gaim since 2001; he
is responsible for Jabber support, among many other things.
-
8/12/2019 bfm%3A978-1-4302-0047-5%2F1
14/19
Acknowledgments
Id like to thank my friends and family for their support while I was writing this book, especiallymy parents Doug and Lynn, and my sister Erin.
Id like to thank everyone at Apress for their support and patience with me, especially
those I worked with directly: Beth Christmas, Jason Gilmore, Candace English, Julie Miller,
and Kelly Winquist.
Mostly, Id like to thank everyone whos contributed to Gaim, making this book possible;
especially Nathan Walp (who is also the technical reviewer for this book), Mark Spencer,
Jim Duchek, Rob Flynn, Syd Logan, Eric Warmenhoven, Adam Fritzler, Benjamin Miller,
Decklin Foster, Mark Doliner, Luke Schierer, Ethan Blanton, Etan Reisner, Tim Ringenbach,
Daniel Atallah, Robert McQueen, Christian Hammond, Herman Bloggs, Stu Tomlinson,
Gary Kramlich, Ka-Hing Cheung, Kevin Stange, and Felipe Contreras.
-
8/12/2019 bfm%3A978-1-4302-0047-5%2F1
15/19
Introduction
Idiscovered Gaim in the AOL Instant Messenger (AIM) user profile of a friend about five yearsago. I didnt know a lot about Linux then. I knew that it was a free implementation of UNIX with
publicly available source code, but my previous experiences with UNIX comprised staring at a
shell prompt, trying to type cryptic commands into a DOS-like console. Likewise, I assumed
that Linux was primitive compared to the high-tech, state-of-the-art Windows 98 I was using
that day when I read my friends profile.
Visit the Gaim Web page, it invited me. I followed the link and found my preconceptions
were wrong. On what I had seen as a platform solely for Web servers and corporate mainframes,
I could perform such everyday tasks as chatting with my friends. I expected nothing but text
commands, but I found Web browsers, e-mail clients, and gamesall with windows to scroll
and buttons to click. Linux no longer looked like DOS; it was an actively developed desktop
operating system. Wanting to contribute to that development process, I quickly downloaded
the Gaim source code and started coding.
At the time, though, I had just started learning how to use my operating system and didnt
know any C (the language Gaim is written in). This didnt stop me; I quickly taught myself all the
skills necessary to write this hugely popular desktop application, using Gaim as an example.
Today, Im the lead developer of the Gaim project, and I work closely with a group of developers
to maintain and enhance the most popular open source instant messaging application on the
planet.
About GaimGaim is a modular instant messaging client that supports a wide variety of IM protocols,
including AIM, ICQ, MSN, Yahoo!, and Jabber. Although it was originally written for Linux, it
now runs on most popular operating systems, including Windows and Mac OS X. Although
exact user statistics are impossible to obtain, its users number in the hundreds of thousands, if
not over a million. Its so popular because, in addition to functionality available in other clients,
it offers many unique features and is infinitely extensible through a powerful plug-in API. Most
importantly in the context of this book is that Gaim is free and open source software.
-
8/12/2019 bfm%3A978-1-4302-0047-5%2F1
16/19
xxii I N T R O D U C T I O N
Figure 1.A screenshot of the Gaim application
Free software and open source software are terms that differ slightly in semantics but
describe essentially the same thing. When downloading Gaim, you are offered the source code.Having unfettered access to the source code is quite valuable for understanding how Gaim
works, and is necessary should you wish to make any modifications. The only major provision
is that you grant this same right to modify your changes. This way, Gaim benefits from
hundreds of talented developers around the world, offering their contributions back to the
community. Because Gaim is built entirely with open source tools, involving yourself in its
development is easy, and it makes an excellent example to learn from.
About This BookThis book will help you to learn the same important programming skills I learned using Gaim
as an example. Ill explore the various techniques and technologies used by Gaim, and afterreading this book you will be able to use what youve learned to create your own networked,
cross-platform desktop application.
This book introduces GTK+, the library used to create Gaims graphical user interface. You
will understand the principles behind it, and will know enough of its API to make changes to
Gaims GUI and to develop interfaces for your own applications.
-
8/12/2019 bfm%3A978-1-4302-0047-5%2F1
17/19
-
8/12/2019 bfm%3A978-1-4302-0047-5%2F1
18/19
xxiv I N T R O D U C T I O N
Chapter 6: Advanced GTK+
Now that youre equipped with a basic of understanding of GTK+, I will explore its internal
workings more thoroughly. You will learn how to create your own GUI objects, or widgets, byfollowing a sample plug-in that radically changes Gaims File Transfer dialog with three such
custom objects.
Chapter 7: Sockets
In Chapter 7, Ill introduce sockets, the programmatic interface to the Internet. You will learn
some basic principles of networking, and how to communicate over a network. I will explain
how to hook your networking code to GTK+s GLib library to integrate it into GTK+s main loop
and ensure cross-platform compatibility.
Chapter 8: Protocol Plug-Ins
After learning how to communicate over the Internet, you will learn about various protocols,
including IM protocols. You will learn how to intercept and interpret Internet traffic used by IM
clientsa process called reverse engineering. I will then teach you how to add a new protocolto Gaim as a protocol plug-in. I will review how Gaim and the plug-in communicate events
between each other.
Chapter 9: Internationalization
In this chapter, you will learn how to write programs that speakers of any language can use. I
will review the gettext system for providing translation of your program at runtime. I will
explain the concept of text encodings, why they are significant, and how to write code that
understands them.
Chapter 10: Portability
Chapter 10 will address portability; that is, making your program run on as many systems aspossible, regardless of hardware or operating system. I will discuss major differences you needto concern yourself with and how to use the portability functions of GTK+.
PrerequisitesI mentioned that when I started working on Gaim I was a complete amateur who didnt even
know the language its programmed in. As such, I dont expect you to be very experienced
either. Youll learn as we go.
However, I wont be introducing basic concepts of C, the language used to build Gaim.
Therefore,you should possess at least a rudimentary understanding of the language, and have
a basic knowledge of underlying principles of computer programming. Although I learned C by
example, there are other books that will do a better job of teaching you This book will focus on
higher-level aspects of the development process. However, you can most likely get by with
knowing C++, Java, or another language with a C-like syntax, as I will elaborate on more-
difficult or obscure techniques.
-
8/12/2019 bfm%3A978-1-4302-0047-5%2F1
19/19
I NT R ODU C T I ON
Because Gaim is created entirely with free software, everything you need is freely available
for download on the Internet. In Chapter 1, you will install the tools youll need for developing
Gaim and your own applications.
With a basic understanding of programming and a few free tools, you should be able to
follow this book and to write large programs like Gaim on your own.
Summary
Gaim has many advantages over other clients, but its major asset is that its open source.
Because of this, anyone with an idea and the ability to code can make it a better program. Also,it acts as a teaching toola real-life, practical example of a large codebase from which you can
learn to write similar code. This book will use Gaims example to teach you exactly that. I amalso available to answer any questions you may have. To e-mail or IM me, see my contact
information at http://gaim.sourceforge.net/contactinfo.php.