File indexing completed on 2024-04-28 16:59:40

0001 /*
0002    Copyright (C) 2013 Andreas Hartmetz <ahartmetz@gmail.com>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LGPL.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 
0019    Alternatively, this file is available under the Mozilla Public License
0020    Version 1.1.  You may obtain a copy of the License at
0021    http://www.mozilla.org/MPL/
0022 */
0023 
0024 #include "arguments.h"
0025 #include "connectaddress.h"
0026 #include "error.h"
0027 #include "eventdispatcher.h"
0028 #include "imessagereceiver.h"
0029 #include "message.h"
0030 #include "pendingreply.h"
0031 #include "connection.h"
0032 
0033 #include <iostream>
0034 #include <string>
0035 
0036 #include "setupeavesdropping.h"
0037 
0038 class ReplyPrinter : public IMessageReceiver
0039 {
0040     // reimplemented from IMessageReceiver
0041     void handleSpontaneousMessageReceived(Message m, Connection *) override;
0042 };
0043 
0044 void ReplyPrinter::handleSpontaneousMessageReceived(Message m, Connection *)
0045 {
0046     std::cout << '\n' << m.prettyPrint();
0047 }
0048 
0049 static void printHelp()
0050 {
0051     std::cout << "dfer options:\n"
0052                  "  --session-bus  Monitor the session bus [the default]\n"
0053                  "  --system-bus   Monitor the system bus\n"
0054                  "  --help         Show this help and exit\n";
0055 }
0056 
0057 int main(int argc, char *argv[])
0058 {
0059     EventDispatcher dispatcher;
0060 
0061     ConnectAddress::StandardBus bus = ConnectAddress::StandardBus::Session;
0062     for (int i = 1; i < argc; i++) {
0063         std::string s = argv[i];
0064         if (s == "--help") {
0065             printHelp();
0066             exit(0);
0067         } else if (s == "--system-bus") {
0068             bus = ConnectAddress::StandardBus::System;
0069         } else if (s == "--session-bus") {
0070             bus = ConnectAddress::StandardBus::Session;
0071         } else {
0072             std::cerr << "Unknown option \"" << s << "\".\n";
0073             printHelp();
0074             exit(1);
0075         }
0076     }
0077 
0078     Connection connection(&dispatcher, bus);
0079 
0080     if (setupEavesdropping(&connection) == FailedEavesdropping) {
0081         return -1;
0082     }
0083 
0084     ReplyPrinter receiver;
0085     connection.setSpontaneousMessageReceiver(&receiver);
0086 
0087     while (true) {
0088         dispatcher.poll();
0089     }
0090 
0091     return 0;
0092 }