| 107 | } |
| 108 | |
| 109 | int Application::exec(int argc, const char *const *argv) |
| 110 | { |
| 111 | try { |
| 112 | // parse arguments |
| 113 | m_args.parser.readArgs(argc, argv); |
| 114 | |
| 115 | // check whether application needs to be terminated due to --bash-completion argument |
| 116 | if (terminated) { |
| 117 | return statusCode; |
| 118 | } |
| 119 | |
| 120 | m_args.parser.ensureDefaultOperation(); |
| 121 | m_args.parser.checkConstraints(); |
| 122 | m_argsRead = true; |
| 123 | |
| 124 | } catch (const ParseError &failure) { |
| 125 | cerr << failure; |
| 126 | return 1; |
| 127 | } |
| 128 | |
| 129 | // handle help argument |
| 130 | if (m_args.parser.helpArg().isPresent()) { |
| 131 | m_args.parser.printHelp(cout); |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | // load configuration |
| 136 | if (const int res = loadConfig()) { |
| 137 | return res; |
| 138 | } |
| 139 | |
| 140 | // finally do the request or establish connection |
| 141 | m_connection.setPollingFlags(SyncthingConnection::PollingFlags::MainEvents); |
| 142 | if (m_args.status.isPresent() || m_args.statusPwd.isPresent() || m_args.waitForIdle.isPresent()) { |
| 143 | // those arguments require establishing a connection first, the actual handler is called by handleStatusChanged() when |
| 144 | // the connection has been established |
| 145 | if (m_args.status.isPresent()) { |
| 146 | connect(&m_connection, &SyncthingConnection::newConfigApplied, [this] { findRelevantDirsAndDevs(OperationType::Status); }); |
| 147 | } |
| 148 | m_connection.reconnect(m_settings); |
| 149 | cerr << Phrases::Info << "Connecting to " << m_settings.syncthingUrl.toLocal8Bit().data() << " ..." << TextAttribute::Reset << flush; |
| 150 | } else { |
| 151 | // call handler for any other arguments directly |
| 152 | applySettings(); |
| 153 | m_args.parser.invokeCallbacks(); |
| 154 | } |
| 155 | |
| 156 | // enter main event loop |
| 157 | if (!m_requiresMainEventLoop) { |
| 158 | return 0; |
| 159 | } |
| 160 | return QCoreApplication::exec(); |
| 161 | } |
| 162 | |
| 163 | void Application::invalidateDirsAndDevs() |
| 164 | { |