KXmlGui

main.cpp
1 /*
2  SPDX-FileCopyrightText: 2000 Bernd Johannes Wuebben <[email protected]>
3  SPDX-FileCopyrightText: 2000 Stephan Kulow <[email protected]>
4 
5  SPDX-License-Identifier: GPL-2.0-or-later
6 */
7 
8 #include "main.h"
9 #include <qplatformdefs.h>
10 
11 #include <QTextStream>
12 #include <QApplication>
13 #include <QHostInfo>
14 
15 #include <kemailsettings.h>
16 #include <klocalizedstring.h>
17 
18 #include <kconfig.h>
19 #include <qcommandlineparser.h>
20 #include <qcommandlineoption.h>
21 
22 #include "smtp.h"
23 #include "../systeminformation_p.h"
24 
25 
26 void BugMailer::slotError(int errornum)
27 {
28  QString lstr;
29 
30  switch (errornum) {
31  case SMTP::ConnectError:
32  lstr = i18n("Error connecting to server.");
33  break;
34  case SMTP::NotConnected:
35  lstr = i18n("Not connected.");
36  break;
37  case SMTP::ConnectTimeout:
38  lstr = i18n("Connection timed out.");
39  break;
40  case SMTP::InteractTimeout:
41  lstr = i18n("Time out waiting for server interaction.");
42  break;
43  default:
44  lstr = QString::fromLatin1(sm->getLastLine().trimmed());
45  lstr = i18n("Server said: \"%1\"", lstr);
46  }
47  // qCDebug(DEBUG_KXMLGUI) << lstr;
48 
49  fputs(lstr.toUtf8().data(), stdout);
50  fflush(stdout);
51 
52  qApp->exit(1);
53 }
54 
55 void BugMailer::slotSend()
56 {
57  // qCDebug(DEBUG_KXMLGUI);
58  qApp->exit(0);
59 }
60 
61 int main(int argc, char **argv)
62 {
63 
64  QCoreApplication a(argc, argv);
65  a.setApplicationName(QStringLiteral("ksendbugmail"));
66  a.setApplicationVersion(QStringLiteral("1.0"));
67 
69 
70  //d.addAuthor(ki18n("Stephan Kulow"), ki18n("Author"), "[email protected]");
71 
72  QString subject, recipient;
73  {
74  QCommandLineParser parser;
75  parser.addVersionOption();
76  parser.setApplicationDescription(i18n("Sends a bug report by email."));
77  parser.addHelpOption();
78  parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("subject"), i18n("The subject line of the email."), QStringLiteral("argument")));
79  parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("recipient"), i18n("The email address to send the bug report to."), QStringLiteral("argument"), QStringLiteral("[email protected]")));
80  parser.process(a);
81  recipient = parser.value(QStringLiteral("recipient"));
82  subject = parser.value(QStringLiteral("subject"));
83  }
84  if (recipient.isEmpty()) {
85  recipient = QStringLiteral("[email protected]");
86  } else {
87  if (recipient.at(0) == QLatin1Char('\'')) {
88  recipient = recipient.mid(1, recipient.length() - 2);
89  }
90  }
91  // qCDebug(DEBUG_KXMLGUI) << "recp" << recipient;
92 
93  if (subject.isEmpty()) {
94  subject = QStringLiteral("(no subject)");
95  } else {
96  if (subject.at(0) == QLatin1Char('\'')) {
97  subject = subject.mid(1, subject.length() - 2);
98  }
99  }
100  QTextStream input(stdin, QIODevice::ReadOnly);
101  input.setCodec("UTF-8");
102  QString text, line;
103  while (!input.atEnd()) {
104  line = input.readLine();
105  text += line + QLatin1String("\r\n");
106  }
107  // qCDebug(DEBUG_KXMLGUI) << text;
108 
109  KEMailSettings emailConfig;
110  emailConfig.setProfile(emailConfig.defaultProfileName());
111  QString fromaddr = emailConfig.getSetting(KEMailSettings::EmailAddress);
112  if (!fromaddr.isEmpty()) {
113  QString name = emailConfig.getSetting(KEMailSettings::RealName);
114  if (!name.isEmpty()) {
115  fromaddr = name + QLatin1String(" <") + fromaddr + QLatin1Char('>');
116  }
117  } else {
118  fromaddr = SystemInformation::userName() + QLatin1Char('@') + QHostInfo::localHostName();
119  }
120  // qCDebug(DEBUG_KXMLGUI) << "fromaddr \"" << fromaddr << "\"";
121 
122  QString server = emailConfig.getSetting(KEMailSettings::OutServer);
123  if (server.isEmpty()) {
124  server = QStringLiteral("bugs.kde.org");
125  }
126 
127  SMTP *sm = new SMTP;
128  BugMailer bm(sm);
129 
130  QObject::connect(sm, &SMTP::messageSent, &bm, &BugMailer::slotSend);
131  QObject::connect(sm, &SMTP::error, &bm, &BugMailer::slotError);
132  sm->setServerHost(server);
133  sm->setPort(25);
134  sm->setSenderAddress(fromaddr);
135  sm->setRecipientAddress(recipient);
136  sm->setMessageSubject(subject);
137  sm->setMessageHeader(QStringLiteral("From: %1\r\nTo: %2\r\n").arg(fromaddr, recipient));
138  sm->setMessageBody(text);
139  sm->sendMessage();
140 
141  int r = a.exec();
142  // qCDebug(DEBUG_KXMLGUI) << "execing " << r;
143  delete sm;
144  return r;
145 }
146 
QString name(const QVariant &location)
QCommandLineOption addVersionOption()
static void setApplicationDomain(const char *domain)
QString defaultProfileName() const
bool isEmpty() const const
QCommandLineOption addHelpOption()
void setProfile(const QString &s)
void setApplicationDescription(const QString &description)
void process(const QStringList &arguments)
QString i18n(const char *text, const TYPE &arg...)
QString mid(int position, int n) const const
const QChar at(int position) const const
int length() const const
bool addOption(const QCommandLineOption &option)
char * data()
QString localHostName()
QString fromLatin1(const char *str, int size)
QString getSetting(KEMailSettings::Setting s) const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString value(const QString &optionName) const const
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Aug 12 2020 22:50:46 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.