~ubuntu-branches/ubuntu/saucy/xmms2/saucy-proposed

« back to all changes in this revision

Viewing changes to doc/tutorial/c++/tut2.cc

  • Committer: Bazaar Package Importer
  • Author(s): Benjamin Drung
  • Date: 2011-10-22 23:53:00 UTC
  • mto: (38.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 43.
  • Revision ID: james.westby@ubuntu.com-20111022235300-u50pdo3g341jvk7q
ImportĀ upstreamĀ versionĀ 0.8+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  XMMS2 - X Music Multiplexer System
 
2
 *  Copyright (C) 2003-2006 XMMS2 Team
 
3
 * 
 
4
 *  This library is free software; you can redistribute it and/or
 
5
 *  modify it under the terms of the GNU Lesser General Public
 
6
 *  License as published by the Free Software Foundation; either
 
7
 *  version 2.1 of the License, or (at your option) any later version.
 
8
 *                   
 
9
 *  This library is distributed in the hope that it will be useful,
 
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 *  Lesser General Public License for more details.
 
13
 *
 
14
 *  This file is a part of the XMMS2 client tutorial #2
 
15
 */
 
16
 
 
17
#include <xmmsclient/xmmsclient++.h>
 
18
 
 
19
#include <iostream>
 
20
#include <cstdlib>
 
21
 
 
22
int
 
23
main()
 
24
{
 
25
 
 
26
        Xmms::Client client("tutorial2");
 
27
 
 
28
        /*
 
29
         * You might've noticed from the previous tutorial
 
30
         * that there wasn't very much error handling involved.
 
31
         * Well, it's not really that simple, most
 
32
         * functions throw three kinds of exceptions,
 
33
         * 
 
34
         *   - Xmms::connection_error
 
35
         *     - Thrown when the client can't connect()
 
36
         *       or if the client is disconnected.
 
37
         *
 
38
         *   - Xmms::result_error
 
39
         *     - Thrown if there was an error getting a
 
40
         *       result from the server.
 
41
         *
 
42
         *   - Xmms::mainloop_running_error
 
43
         *     - (a logic_error) Thrown if the mainloop
 
44
         *       is running and program calls a synchronized
 
45
         *       function (a bad thing). Shouldn't be caught.
 
46
         *
 
47
         * All exceptions are derived from std::runtime_error, except
 
48
         * the mainloop_running_error, which is std::logic_error.
 
49
         * (A C++ coder should know what this means basically,
 
50
         * go learn if you don't.)
 
51
         */
 
52
 
 
53
        try {
 
54
 
 
55
                client.connect( std::getenv( "XMMS_PATH" ) );
 
56
 
 
57
        }
 
58
        /*
 
59
         * Connection might fail and if it does, it will throw
 
60
         * an Xmms::connection_error.
 
61
         *
 
62
         * As every good C++ coder knows, always catch exceptions by reference.
 
63
         */
 
64
        catch( Xmms::connection_error& err ) {
 
65
 
 
66
                std::cout << "Connection failed: " << err.what() << std::endl;
 
67
 
 
68
                /*
 
69
                 * If we don't quit here, all functions will fail with the
 
70
                 * same error.
 
71
                 */
 
72
                return EXIT_FAILURE;
 
73
 
 
74
        }
 
75
 
 
76
        try {
 
77
 
 
78
                /*
 
79
                 * To fetch the current playing id we use this method.
 
80
                 * Xmms::Playback::currentID() (and all others) actually
 
81
                 * returns a special class which implicitly converts to
 
82
                 * the appropriate type in synchronous mode. This will
 
83
                 * be handled in detail in later tutorials.
 
84
                 */
 
85
                int id = client.playback.currentID();
 
86
                std::cout << "Currently playing ID is " << id << std::endl;
 
87
 
 
88
        }
 
89
        /*
 
90
         * As mentioned earlier, all other methods sync methods
 
91
         * (except Xmms::Client::connect and Xmms::Client::quit)
 
92
         * will throw Xmms::result_error if an error occurs.
 
93
         */
 
94
        catch( Xmms::result_error& err ) {
 
95
 
 
96
                std::cout << "playback start returned error, "
 
97
                          << err.what() << std::endl;
 
98
                return EXIT_FAILURE;
 
99
 
 
100
        }
 
101
 
 
102
        /*
 
103
         * Error handling with asynchronous client and
 
104
         * more complex types will be commented on later.
 
105
         */
 
106
 
 
107
        return EXIT_SUCCESS;
 
108
 
 
109
}