More work on scriptable service. can now add albums and tracks via scripts. Script...
[amarok.git] / HACKING
blob9f9011021f57e620b3940e32aeaaef2f9c454d24
1 Hacking on Amarok
2 -----------------
4 Please respect these guidelines when coding for Amarok, thanks!
6 * Where this document isn't clear, refer to Amarok code.
9 This C++ FAQ is a life saver in many situations, so you want to keep it handy:
11   http://www.parashift.com/c++-faq-lite/
14 Formatting
15 ----------
16 * Spaces, not tabs
17 * Indentation is 4 spaces
18 * Lines should be limited to 90 characters
19 * Spaces between brackets and argument functions
20 * For pointer and reference variable declarations put a space between the type
21   and the * or & and no space before the variable name.
22 * For if, else, while and similar statements put the brackets on the next line,
23   although brackets are not needed for single statements.
24 * Function and class definitions have their brackets on separate lines
25 * A function implementation's return type is on its own line.
26 * amarok.h contains some helpful macros, foreach and foreachType.  Use them,
27   they improve coding style and readability.
28 * CamelCase.{cpp,h} style file names.
30 Example:
32  | bool 
33  | MyClass::myMethod( QPtrList<QListViewItem> items, const QString &name )
34  | {
35  |     if( items.isEmpty() )
36  |         return false;
37  |
38  |     foreachType( QPtrList<QListViewItem>, items )
39  |     {
40  |         (*it)->setText( 0, name );
41  |         debug() << "Setting item name: " << name << endl;
42  |     }
43  | }
45 Header includes should be listed in the following order:
46     - Amarok includes
47     - KDE includes
48     - Qt includes
50 They should also be sorted alphabetically, for ease of locating them.  A small comment
51 if applicable is also helpful.
53 Includes in a header file should be kept to the absolute minimum, as to keep compile times
54 low. This can be achieved by using "forward declarations" instead of includes, like
55 "class QListView;" Forward declarations work for pointers and const references.
57 TIP:
58 Kate/KDevelop users can sort the headers automatically. Select the lines you want to sort,
59 then Tools -> Filter Selection Through Command -> "sort".
62 Example:
64  | #include "amarok.h"
65  | #include "debug.h"
66  | #include "playlist.h"
67  |
68  | #include <KDialogBase>    //baseclass
69  | #include <KPushButton>    //see function...
70  |
71  | #include <QGraphicsView>
72  | #include <QWidget>
75 Comments
76 --------
77 Comment your code. Don't comment what the code does, comment on the purpose of the code. It's
78 good for others reading your code, and ultimately it's good for you too.
80 Comments are essential when adding a strange hack, like the following example:
82  | /** Due to xine-lib, we have to make KProcess close all fds, otherwise we get "device is busy" messages
83  |   * Used by AmarokProcIO and AmarokProcess, exploiting commSetupDoneC(), a virtual method that
84  |   * happens to be called in the forked process
85  |   * See bug #103750 for more information.
86  |   */
87  | class AmarokProcIO : public K3ProcIO 
88  | {
89  |     public:
90  |     virtual int commSetupDoneC() {
91  |         const int i = K3ProcIO::commSetupDoneC();
92  |         Amarok::closeOpenFiles(K3ProcIO::out[0],K3ProcIO::in[0],K3ProcIO::err[0]);
93  |         return i;
94  |     }
95  | };
98 For headers, use the Doxygen syntax. See: http://www.stack.nl/~dimitri/doxygen/
100 Example:
102  | /**
103  |  * Start playback.
104  |  * @param offset Start playing at @p msec position.
105  |  * @return True for success.
106  |  */
107  | virtual bool play( uint offset = 0 ) = 0;
110 Header Formatting
111 -----------------
112 General rules apply here.  Please keep header function definitions aligned nicely,
113 if possible.  It helps greatly when looking through the code.  Sorted methods,
114 either by name or by their function (ie, group all related methods together) is
115 great too.
118  | #ifndef AMAROK_QUEUEMANAGER_H
119  | #define AMAROK_QUEUEMANAGER_H
121  | class QueueList : public K3ListView
122  | {
123  |         Q_OBJECT
125  |     public:
126  |         Queuelist( QWidget *parent, const char *name = 0 );
127  |         ~QueueList() {};
129  |     public slots:
130  |         void    moveSelectedUp();
131  |         void    moveSelectedDown();
132  | };
134 #endif /* AMAROK_QUEUEMANAGER_H */
137 0 vs NULL
138 ---------
139 The use of 0 to express a null pointer is preferred over the use of NULL.
140 0 is not a magic value, it's the defined value of the null pointer in C++.
141 NULL, on the other hand, is a preprocessor directive (#define) and not only is
142 it more typing than '0' but preprocessor directives are less elegant.
144  |     SomeClass *instance = 0;
147 Const Correctness
148 -----------------
149 Try to keep your code const correct. Declare methods const if they don't mutate the object,
150 and use const variables. It improves safety, and also makes it easier to understand the code.
152 See: http://www.parashift.com/c++-faq-lite/const-correctness.html
155 Example:
157  | bool 
158  | MyClass::isValidFile( const QString& path ) const
159  | {
160  |     const bool valid = QFile::exist( path );
162  |     return valid;
163  | }
166 Debugging
167 ---------
168 debug.h contains some handy functions for our debug console output.
169 Please use them instead of kDebug().
171 Usage:
173  | #include "debug.h"
175  | debug()   << "Something is happening" << endl;
176  | warning() << "Something bad may happen" << endl;
177  | error()   << "Something bad did happen!" << endl;
179 Additionally, there are some macros for debugging functions:
181 DEBUG_BLOCK
182 DEBUG_FUNC_INFO
183 DEBUG_LINE_INFO
184 DEBUG_INDENT
185 DEBUG_UNINDENT
187 AMAROK_NOTIMPLEMENTED
188 AMAROK_DEPRECATED
190 threadweaver.h has two additional macros:
191 DEBUG_THREAD_FUNC_INFO outputs the memory address of the current QThread or 'none'
192     if its the original GUI thread.
193 SHOULD_BE_GUI outputs a warning message if it occurs in a thread that isn't in
194     the original "GUI Thread", otherwise it is silent. Useful for documenting
195     functions and to prevent problems in the future.
198 Usage of Amarok::config()
199 -------------------------
200 We provide this method for convenience, but it is important to use it properly. By
201 inspection, we can see that we may produce very obscure bugs in the wrong case:
203  | KConfig 
204  | *config( const QString &group )
205  | {
206  |    //Slightly more useful config() that allows setting the group simultaneously
207  |    KGlobal::config()->setGroup( group );
208  |    return KGlobal::config();
209  | }
211 Take the following example:
213  | void
214  | f1()
215  | {
216  |    KConfig *config = Amarok::config( "Group 2" );
217  |    config->writeEntry( "Group 2 Variable", true );
218  | }
220  | void
221  | doStuff()
222  | {
223  |   KConfig *config = Amarok::config( "Group 1" );
224  |   f1();
225  |   config->writeEntry( "Group 1 Variable", true );
226  | }
228 We would expect the following results:
230  | [Group 1]
231  | Group 1 Variable = true
233  | [Group 2]
234  | Group 2 Variable = true
236 However because the config group is changed before writing the entry:
237  | [Group 1]
239  | [Group 2]
240  | Group 1 Variable = true
241  | Group 2 Variable = true
243 Which is clearly incorrect. And hard to see when your wondering why f1() is not
244 working. So do not store a value of Amarok::config, make it a habit to just 
245 always call writeEntry or readEntry directly.
247 Correct:
248 | amarok::config( "Group 1" )->writeEntry( "Group 1 Variable", true );
251 Errors & Asserts
252 ----------------
253 *Never use assert() or fatal(). There must be a better option than crashing a user's
254 application (its not uncommon for end-users to have debugging enabled).
256 *KMessageBox is fine to use to prompt the user, but do not use it to display errors
257 or informational messages. Instead, KDE::StatusBar has a few handy methods. Refer to
258 amarok/src/statusbar/statusBarBase.h
261 Copyright
262 ---------
263 To comply with the GPL, add your name, email address & the year to the top of any file
264 that you edit. If you bring in code or files from elsewhere, make sure its
265 GPL-compatible and to put the authors name, email & copyright year to the top of
266 those files.
269 Thanks, now have fun!
270   -- the Amarok developers