Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error messages. #25

Merged
merged 7 commits into from
May 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ script:
- CTEST_OUTPUT_ON_FAILURE=1 make test

after_success:
- coveralls --root .. --build-root . -e tests/ -E .*googletest.* -E .*CMakeFiles.* -E .+/moc_.* -E .+/mocs_.* -E .+/qrc_.* -E .+/qxt.* -E .+/ui_.* > coveralls.log
- coveralls --root .. --build-root . -e tests/ -E .*googletest.* -E .*glog.* -E .*CMakeFiles.* -E .+/moc_.* -E .+/mocs_.* -E .+/qrc_.* -E .+/qxt.* -E .+/ui_.* > coveralls.log
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ else ()
set(gtest_disable_pthreads ON)
endif ()

# Set gflags off.
set(WITH_GFLAGS OFF CACHE BOOL "Use gflags")

# Coverage.
option(COVERAGE "Generate coverage data" OFF)
if (COVERAGE)
Expand All @@ -32,6 +35,13 @@ if (COVERAGE)
endif ()
endif ()

# Add a logging tool.
include(cmake/glog.cmake)
fetch_glog(
${PROJECT_SOURCE_DIR}/cmake
${PROJECT_BINARY_DIR}/glog
)

# Add source folder.
add_subdirectory(src)

Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# instant-translator [![Build Status](https://travis-ci.org/ugnelis/instant-translator.svg?branch=master)](https://travis-ci.org/ugnelis/instant-translator) [![Coverage Status](https://coveralls.io/repos/github/ugnelis/instant-translator/badge.svg?branch=master)](https://coveralls.io/github/ugnelis/instant-translator?branch=master)
# instant-translator
[![Build Status](https://travis-ci.org/ugnelis/instant-translator.svg?branch=master)](https://travis-ci.org/ugnelis/instant-translator)
[![Coverage Status](https://coveralls.io/repos/github/ugnelis/instant-translator/badge.svg?branch=master)](https://coveralls.io/github/ugnelis/instant-translator?branch=master)

Translate words and sentences instantly. Program uses APIs of various translators.

## What Does the App Solve?
Expand Down Expand Up @@ -27,6 +30,12 @@ key=AsjSAXXX
format=text
```

### Turn On Logs
In this project gflags library isn't used. That is why environment variable `GLOG_logtostderr` has to be set in order to see logs.
```bash
GLOG_logtostderr=1 ./instant_translator
```

### Translate by Pressing the Keyboard
Currently, it's made that when clipboard (copy or cut operation) is changed then the app translates the text.

Expand Down
17 changes: 17 additions & 0 deletions cmake/glog-download.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

project(glog-download NONE)

include(ExternalProject)

ExternalProject_Add(glog
SOURCE_DIR "@GLOG_DOWNLOAD_ROOT@/glog-src"
BINARY_DIR "@GLOG_DOWNLOAD_ROOT@/glog-build"
GIT_REPOSITORY
https://github.com/google/glog.git
GIT_TAG
v0.3.5
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND "")
25 changes: 25 additions & 0 deletions cmake/glog.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
macro(fetch_glog _download_module_path _download_root)
set(GLOG_DOWNLOAD_ROOT ${_download_root})
configure_file(
${_download_module_path}/glog-download.cmake
${_download_root}/CMakeLists.txt
@ONLY)

unset(GLOG_DOWNLOAD_ROOT)

execute_process(
COMMAND
"${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" .
WORKING_DIRECTORY
${_download_root})

execute_process(
COMMAND
"${CMAKE_COMMAND}" --build .
WORKING_DIRECTORY
${_download_root})

add_subdirectory(
${_download_root}/glog-src
${_download_root}/glog-build)
endmacro()
5 changes: 4 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ set(QT5_LIBRARIES
Qt5::Network
Qt5::Concurrent)

set(OTHER_LIBRARIES
glog)

# UI files.
qt5_wrap_ui(UI_HEADERS mainwindow.ui)

Expand All @@ -52,4 +55,4 @@ set(SOURCE_FILES
add_executable(instant_translator ${SOURCE_FILES} ${RESOURCE_FILES_ADDED} ${UI_HEADERS})

# Use the Widgets module from Qt5.
target_link_libraries(instant_translator ${QT5_LIBRARIES})
target_link_libraries(instant_translator ${QT5_LIBRARIES} ${OTHER_LIBRARIES})
26 changes: 24 additions & 2 deletions src/apis/googleapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ QString GoogleAPI::translate(const QString &input,

QNetworkAccessManager *manager = new QNetworkAccessManager();

RequestManager requestManager(nullptr, std::move(manager));
RequestManager requestManager(nullptr, manager);
requestManager.postRequest(request, postJsonDocument.toJson());

// Parse the replay.
Expand All @@ -47,6 +47,17 @@ QString GoogleAPI::translate(const QString &input,
QJsonDocument replyJsonDocument = QJsonDocument::fromJson(replyByteArray.data());
QJsonObject replyJsonObject = replyJsonDocument.object();

// If error.
if (replyJsonObject.contains("error")) {
QJsonObject errorJsonObject = replyJsonObject["error"].toObject();
int errorCode = errorJsonObject["code"].toInt();
std::string errorMessage = errorJsonObject["message"]
.toString()
.toStdString();
std::string exceptionMessage = "Error " + std::to_string(errorCode) + ": " + errorMessage;
throw std::invalid_argument(exceptionMessage);
}

QJsonArray translationsJsonArray = replyJsonObject["data"]
.toObject()["translations"]
.toArray();
Expand Down Expand Up @@ -74,7 +85,7 @@ QStringList GoogleAPI::getSupportedLanguages() const {

QNetworkAccessManager *manager = new QNetworkAccessManager();

RequestManager requestManager(nullptr, std::move(manager));
RequestManager requestManager(nullptr, manager);
requestManager.getRequest(request);

// Parse the replay.
Expand All @@ -83,6 +94,17 @@ QStringList GoogleAPI::getSupportedLanguages() const {
QJsonDocument replyJsonDocument = QJsonDocument::fromJson(replyByteArray.data());
QJsonObject replyJsonObject = replyJsonDocument.object();

// If error.
if (replyJsonObject.contains("error")) {
QJsonObject errorJsonObject = replyJsonObject["error"].toObject();
int errorCode = errorJsonObject["code"].toInt();
std::string errorMessage = errorJsonObject["message"]
.toString()
.toStdString();
std::string exceptionMessage = "Error " + std::to_string(errorCode) + ": " + errorMessage;
throw std::invalid_argument(exceptionMessage);
}

QJsonArray languagesJsonArray = replyJsonObject["data"]
.toObject()["languages"]
.toArray();
Expand Down
1 change: 1 addition & 0 deletions src/apis/googleapi.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef INSTANT_TRANSLATOR_GOOGLEAPI_H
#define INSTANT_TRANSLATOR_GOOGLEAPI_H

#include <stdexcept>
#include <QObject>
#include <QSettings>
#include <QJsonDocument>
Expand Down
4 changes: 4 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#include <QApplication>
#include <QSettings>
#include <glog/logging.h>
#include "mainwindow.h"

int main(int argc, char *argv[]) {
// Logging tool.
google::InitGoogleLogging(argv[0]);

QApplication application(argc, argv);

// Settings.
Expand Down
26 changes: 24 additions & 2 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,44 @@ QString MainWindow::runTranslation(API *api,
const QString &sourceLanguage,
const QString &targetLanguage) {
if (api == nullptr) {
showErrorBox("Text cannot be translated. API is not set.");
return QString();
}

QString outputString = api->translate(inputString, sourceLanguage, targetLanguage);
QString outputString;
try {
outputString = api->translate(inputString, sourceLanguage, targetLanguage);
} catch (const std::invalid_argument &e) {
showErrorBox(e.what());
}

return outputString;
}

QStringList MainWindow::runGetSupportedLanguages(API *api) {
if (api == nullptr) {
showErrorBox("Languages list cannot be received. API is not set.");
return QStringList();
}

QStringList supportedLanguages = api->getSupportedLanguages();
QStringList supportedLanguages;
try {
supportedLanguages = api->getSupportedLanguages();
} catch (const std::invalid_argument &e) {
showErrorBox(e.what());
}

return supportedLanguages;
}

void MainWindow::showErrorBox(const QString &message) {
DLOG(INFO) << message.toStdString();
QMessageBox messageBox;
messageBox.setText(message);
messageBox.setIcon(QMessageBox::Warning);
messageBox.exec();
}

void MainWindow::on_exitAction_triggered() {
this->close();
}
Expand Down
9 changes: 9 additions & 0 deletions src/mainwindow.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <stdexcept>
#include <QMainWindow>
#include <QClipboard>
#include <QFuture>
#include <QFutureWatcher>
#include <QtConcurrent>
#include <QStringList>
#include <QMessageBox>
#include <glog/logging.h>
#include "apis/api.h"
#include "apis/googleapi.h"
#include "utils/language.h"
Expand Down Expand Up @@ -92,6 +95,12 @@ private slots:
*/
QStringList runGetSupportedLanguages(API *api);

/**
* Show error box.
* @param message Error message.
*/
void showErrorBox(const QString &message);

Ui::MainWindow *ui; // MainWindow user interface.
QClipboard *clipboard; // Clipboard information.
API *api; // Translation API.
Expand Down