Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
GitHubUserFetcherWindow Class Reference

Main application window for the GitHub user fetcher GUI. More...

+ Inheritance diagram for GitHubUserFetcherWindow:

Public Member Functions

 GitHubUserFetcherWindow ()
 Constructs the main window, sets up widgets and signals.
 

Private Member Functions

void on_button_clicked ()
 Event handler for the fetch button.
 
void show_message (const std::string &msg)
 Displays a message in the text view area.
 

Private Attributes

Gtk::Box box
 Container for layout.
 
Gtk::Entry entry
 Text field for GitHub username input.
 
Gtk::Button button
 Button to trigger fetch.
 
Gtk::ScrolledWindow scrolled_window
 Scrollable container for text_view.
 
Gtk::TextView text_view
 Text view to display results or messages.
 

Detailed Description

Main application window for the GitHub user fetcher GUI.

This class sets up the UI elements such as the input field, button, and display area. It fetches user data from GitHub in a background thread and updates the UI asynchronously using Glib's main loop.

Definition at line 39 of file gui.cpp.

Constructor & Destructor Documentation

◆ GitHubUserFetcherWindow()

GitHubUserFetcherWindow::GitHubUserFetcherWindow ( )
inline

Constructs the main window, sets up widgets and signals.

Applies CSS styling, initializes all widgets, and connects the button click signal to the event handler.

Definition at line 47 of file gui.cpp.

47 {
48 auto css_provider = Gtk::CssProvider::create();
49 try {
50 css_provider->load_from_data(css_data);
51 auto display = Gdk::Display::get_default();
52 Gtk::StyleContext::add_provider_for_display(
53 display, css_provider, GTK_STYLE_PROVIDER_PRIORITY_USER);
54 } catch (const Glib::FileError &e) {
55 std::cerr << "Failed to load CSS data: " << e.what() << '\n';
56 } catch (...) {
57 std::cerr << "Unknown error occurred while loading CSS.\n";
58 }
59
60 set_title("GitHub User Fetcher");
61 set_default_size(400, 300);
62
63 box.set_orientation(Gtk::Orientation::VERTICAL);
64 set_child(box);
65
66 entry.set_placeholder_text("Enter GitHub username...");
67 box.append(entry);
68
69 button.set_label("Fetch User");
70 box.append(button);
71
72 scrolled_window.set_child(text_view);
73 scrolled_window.set_policy(Gtk::PolicyType::AUTOMATIC,
74 Gtk::PolicyType::AUTOMATIC);
75 scrolled_window.set_expand(true);
76 box.append(scrolled_window);
77
78 text_view.set_editable(false);
79 text_view.set_wrap_mode(Gtk::WrapMode::WORD);
80
81 button.signal_clicked().connect(
82 sigc::mem_fun(*this, &GitHubUserFetcherWindow::on_button_clicked));
83 }
Gtk::TextView text_view
Text view to display results or messages.
Definition gui.cpp:90
Gtk::Box box
Container for layout.
Definition gui.cpp:86
Gtk::Button button
Button to trigger fetch.
Definition gui.cpp:88
void on_button_clicked()
Event handler for the fetch button.
Definition gui.cpp:98
Gtk::ScrolledWindow scrolled_window
Scrollable container for text_view.
Definition gui.cpp:89
Gtk::Entry entry
Text field for GitHub username input.
Definition gui.cpp:87
#define GTK_STYLE_PROVIDER_PRIORITY_USER

References box, button, entry, GTK_STYLE_PROVIDER_PRIORITY_USER, on_button_clicked(), scrolled_window, and text_view.

Member Function Documentation

◆ on_button_clicked()

void GitHubUserFetcherWindow::on_button_clicked ( )
inlineprivate

Event handler for the fetch button.

Starts a background thread to fetch GitHub user data and updates the text view with the results. Displays an error message on failure.

Definition at line 98 of file gui.cpp.

98 {
99 std::string username = entry.get_text();
100 if (username.empty()) {
101 show_message("Please enter a username.");
102 return;
103 }
104
105 text_view.get_buffer()->set_text("Fetching...");
106
107 std::thread([this, username]() {
108 try {
109 User user = User::fetch_github_user(username);
110 nlohmann::json json = {{"login", user.login},
111 {"name", user.name},
112 {"company", user.company},
113 {"location", user.location}};
114
115 std::string result = json.dump(4);
116
117 Glib::signal_idle().connect_once(
118 [this, result]() { text_view.get_buffer()->set_text(result); });
119
120 } catch (const std::exception &e) {
121 std::string error_msg = std::string("Error: ") + e.what();
122 Glib::signal_idle().connect_once([this, error_msg]() {
123 text_view.get_buffer()->set_text(error_msg);
124 });
125 }
126 }).detach();
127 }
void show_message(const std::string &msg)
Displays a message in the text view area.
Definition gui.cpp:133
Structure representing a GitHub user.
Definition user.h:9
const char * name
Full name.
Definition user.h:11
const char * login
GitHub username.
Definition user.h:10
const char * location
User's location.
Definition user.h:13
const char * company
Company name.
Definition user.h:12

References User::company, entry, User::location, User::login, User::name, show_message(), and text_view.

Referenced by GitHubUserFetcherWindow().

◆ show_message()

void GitHubUserFetcherWindow::show_message ( const std::string & msg)
inlineprivate

Displays a message in the text view area.

Parameters
msgThe message to display.

Definition at line 133 of file gui.cpp.

133 {
134 text_view.get_buffer()->set_text(msg);
135 }

References text_view.

Referenced by on_button_clicked().

Field Documentation

◆ box

Gtk::Box GitHubUserFetcherWindow::box
private

Container for layout.

Definition at line 86 of file gui.cpp.

Referenced by GitHubUserFetcherWindow().

◆ button

Gtk::Button GitHubUserFetcherWindow::button
private

Button to trigger fetch.

Definition at line 88 of file gui.cpp.

Referenced by GitHubUserFetcherWindow().

◆ entry

Gtk::Entry GitHubUserFetcherWindow::entry
private

Text field for GitHub username input.

Definition at line 87 of file gui.cpp.

Referenced by GitHubUserFetcherWindow(), and on_button_clicked().

◆ scrolled_window

Gtk::ScrolledWindow GitHubUserFetcherWindow::scrolled_window
private

Scrollable container for text_view.

Definition at line 89 of file gui.cpp.

Referenced by GitHubUserFetcherWindow().

◆ text_view

Gtk::TextView GitHubUserFetcherWindow::text_view
private

Text view to display results or messages.

Definition at line 90 of file gui.cpp.

Referenced by GitHubUserFetcherWindow(), on_button_clicked(), and show_message().


The documentation for this class was generated from the following file: