Skip to content

Commit 94790b9

Browse files
committed
chore(logs): removed unused logs and make notification optional with configs.
1 parent 5a9dff6 commit 94790b9

8 files changed

Lines changed: 119 additions & 35 deletions

File tree

src/audio/player.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "../common/Track.h"
44
#include "../core/config/config.hpp"
5+
#include "../common/notification.hpp"
56
// #include "visualizer.hpp"
67
#include <algorithm>
78
#include <atomic>
@@ -67,7 +68,8 @@ class MusicPlayer {
6768

6869
// Logging utility
6970
void log_error(const std::string &message) {
70-
std::cerr << "[MusicPlayer Error] " << message << std::endl;
71+
// std::cerr << "[MusicPlayer Error] " << message << std::endl;
72+
notifications::send("[MusicPlayer Error] " + message);
7173
}
7274

7375
static size_t write_callback(void *ptr, size_t size, size_t nmemb,
@@ -223,8 +225,9 @@ class MusicPlayer {
223225
const char *cmd[] = {"loadfile", playlist[0].c_str(), NULL};
224226
int result = mpv_command(mpv.get(), cmd);
225227
if (result < 0) {
226-
std::cerr << "Failed to load first track. Error code: " << result
227-
<< std::endl;
228+
// std::cerr << "Failed to load first track. Error code: " << result
229+
// << std::endl;
230+
notifications::send("Failed to load first track. Error code: " + std::to_string(result));
228231
return;
229232
}
230233
}
@@ -372,7 +375,7 @@ class MusicPlayer {
372375
// -x: Extract audio
373376
// --audio-format mp3: Convert to MP3
374377
// -o: Output template
375-
std::string command = "yt-dlp -x --audio-format mp3 -o \"" +
378+
std::string command = "yt-dlp -q -x --audio-format mp3 -o \"" +
376379
final_path + "\" \"" + url + "\"";
377380

378381
/* std::string command = */
@@ -387,14 +390,15 @@ class MusicPlayer {
387390
}
388391

389392
// Notify completion
390-
system(
391-
("notify-send \"Download completed: " + final_path + "\"").c_str());
393+
// system(("notify-send \"Download completed: " + final_path + "\"").c_str());
394+
notifications::send_download_complete("" + final_path);
392395

393396
} catch (const std::exception &e) {
394397
log_error(e.what());
395-
system(
396-
("notify-send \"Download failed: " + std::string(e.what()) + "\"")
397-
.c_str());
398+
// system(
399+
// ("notify-send \"Download failed: " + std::string(e.what()) + "\"")
400+
// .c_str());
401+
notifications::send_download_failed("" + std::string(e.what()));
398402
}
399403

400404
is_downloading = false;
@@ -532,7 +536,7 @@ class MusicPlayer {
532536
// visualization_data = visualizer->process(audio_buffer);
533537
}
534538
// In the audio data handling section:
535-
std::cerr << "Audio data received: " << num_samples << " samples" << std::endl;
539+
// std::cerr << "Audio data received: " << num_samples << " samples" << std::endl;
536540
// std::cerr << "Visualization data size: " << visualization_data.size() << std::endl;
537541

538542

src/common/notification.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "notification.hpp"
2+
3+
namespace notifications {
4+
// All implementation is in the header file (inline functions)
5+
}

src/common/notification.hpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <cstdlib>
5+
#include "../core/config/config.hpp"
6+
7+
namespace notifications {
8+
9+
// Global config pointer - to be set by main()
10+
inline Config* g_config = nullptr;
11+
12+
inline void init(Config* cfg) {
13+
g_config = cfg;
14+
}
15+
16+
inline void send(const std::string& message) {
17+
// Check if config is initialized
18+
if (g_config == nullptr) {
19+
// Fallback: send notification anyway if config not available
20+
system(("notify-send \"tuisic\" \"" + message + "\"").c_str());
21+
return;
22+
}
23+
24+
// Check config setting
25+
if (g_config->get_notifications_enabled()) {
26+
system(("notify-send \"tuisic\" \"" + message + "\"").c_str());
27+
}
28+
}
29+
30+
inline void send_download_complete(const std::string& track_name) {
31+
send("Download completed: " + track_name);
32+
}
33+
34+
inline void send_download_failed(const std::string& error) {
35+
send("Download failed: " + error);
36+
}
37+
38+
inline void send_download_started(const std::string& track_name) {
39+
send("Started downloading: " + track_name);
40+
}
41+
42+
inline void send_url_copied() {
43+
send("URL copied to clipboard");
44+
}
45+
46+
inline void send_daemon_started() {
47+
send("tuisic daemon started");
48+
}
49+
50+
} // namespace notifications

src/core/config/config.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ class Config {
190190
}
191191

192192
bool get_subtitle_enabled() const {
193-
return get_bool_value("player", "subtitle_enabled", true);
193+
return get_bool_value("player", "subtitle_enabled", false);
194194
}
195195

196196
int get_volume() const { return get_int_value("player", "volume", 100); }

src/core/main.cpp

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
#include <unistd.h>
3636
#include <vector>
3737

38+
#include "../common/notification.hpp"
39+
3840
// Data in string to render in UI
3941
std::vector<std::string> track_strings;
4042
std::vector<std::string> home_track_strings;
@@ -231,7 +233,9 @@ void setupMPRISForDaemon(std::shared_ptr<MusicPlayer> player,
231233
int current_track_index) {
232234
mpris_handler = std::make_unique<MPRISHandler>(player);
233235
mpris_handler->initialize();
234-
system("notify-send 'MPRIS integration initialized'");
236+
//system("notify-send 'MPRIS integration initialized'");
237+
notifications::send("MPRIS integration initialized");
238+
235239

236240
mpris_handler->setTrackCallbacks(
237241
[&]() -> std::string {
@@ -385,7 +389,8 @@ int main(int argc, char *argv[]) {
385389
};
386390

387391
auto updateMetadata = [&]() {
388-
system(("notify-send 'Tuisic' 'Updating metadata'"));
392+
//system(("notify-send 'Tuisic' 'Updating metadata'"));
393+
notifications::send("updating songs");
389394
std::map<std::string, sdbus::Variant> properties;
390395
properties["Metadata"] = sdbus::Variant(getMetadata());
391396
g_concatenator->emitPropertiesChangedSignal(interfaceName2);
@@ -525,6 +530,9 @@ int main(int argc, char *argv[]) {
525530
curl_global_init(CURL_GLOBAL_ALL);
526531
auto config = std::make_shared<Config>();
527532

533+
// Initialize notification system with config
534+
notifications::init(config.get());
535+
528536
using namespace ftxui;
529537

530538
// Placeholder for search input
@@ -675,7 +683,8 @@ int main(int argc, char *argv[]) {
675683

676684
screen.PostEvent(Event::Custom);
677685
} catch (const std::exception &e) {
678-
std::cerr << e.what() << std::endl;
686+
// std::cerr << e.what() << std::endl;
687+
notifications::send("Error: " + std::string(e.what()));
679688
}
680689
});
681690
next_tracks_thread.detach();
@@ -695,19 +704,21 @@ int main(int argc, char *argv[]) {
695704
std::string is_wayland = getenv("XDG_SESSION_TYPE");
696705
if (is_wayland == "wayland") {
697706
system(("echo \"" + url + "\" | wl-copy").c_str());
698-
system(("notify-send \"Copied URL: " + url + "\"").c_str());
699-
std::cout << "Copied URL: " << url << std::endl;
707+
//system(("notify-send \"Copied URL: " + url + "\"").c_str());
708+
notifications::send_url_copied();
709+
//std::cout << "Copied URL: " << url << std::endl;
700710
return true;
701711
}
702-
system(
703-
("echo \"" + url + "\" | xclip -selection clipboard").c_str());
704-
system(("notify-send \"Copied URL: " + url + "\"").c_str());
705-
std::cout << "Copied URL: " << url << std::endl;
712+
system(("echo \"" + url + "\" | xclip -selection clipboard").c_str());
713+
//system(("notify-send \"Copied URL: " + url + "\"").c_str());
714+
notifications::send_url_copied();
715+
//std::cout << "Copied URL: " << url << std::endl;
706716
return true;
707717
}
708718
}
709719
if (event == Event::Character('L')) {
710-
std::cerr << "pressed L" << std::endl;
720+
//std::cerr << "pressed L" << std::endl;
721+
notifications::send("toggle subtitles");
711722
player->toggle_subtitles();
712723
}
713724

@@ -732,20 +743,24 @@ int main(int argc, char *argv[]) {
732743
// Start download
733744
if (player->download_track(track_data[selected].url, path,
734745
current_song)) {
735-
system(("notify-send \"Started downloading: " +
736-
track_data[selected].name + "\"")
737-
.c_str());
746+
// system(("notify-send \"Started downloading: " +
747+
// track_data[selected].name + "\"")
748+
// .c_str());
749+
notifications::send_download_started("" + track_data[selected].name);
738750
} else {
739-
system(("notify-send \"Failed to start download: " +
740-
track_data[selected].name + "\"")
741-
.c_str());
751+
// system(("notify-send \"Failed to start download: " +
752+
// track_data[selected].name + "\"")
753+
// .c_str());
754+
notifications::send_download_failed("" + track_data[selected].name);
755+
742756
}
743757
}
744758
return true;
745759
}
746760
if (event == Event::Character('p')) {
747761
std::string some = config->get_download_path();
748-
system(("notify-send \"Download path: " + some + "\"").c_str());
762+
//system(("notify-send \"Download path: " + some + "\"").c_str());
763+
notifications::send("Download path: " + some);
749764
}
750765

751766
if (event == Event::Character('r')) {
@@ -756,7 +771,8 @@ int main(int argc, char *argv[]) {
756771
#ifdef WITH_MPRIS
757772
daemon_mode_active = true;
758773
//system(("notify-send \"Starting daemon..." + track[0].id.c_str() + "\"").c_str());
759-
system(("notify-send 'Starting daemon...'"));
774+
//system(("notify-send 'Starting daemon...'"));
775+
notifications::send("Starting daemon...");
760776

761777

762778
std::string url = player->get_current_track();

src/services/justmusic/justmusic.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <regex>
55
#include <string>
66
#include <vector>
7+
#include "../../common/notification.hpp"
78

89
class Justmusic {
910
public:
@@ -85,7 +86,8 @@ class Justmusic {
8586
tracks.push_back(track);
8687
// std::cout << tracks.back().url << std::endl;
8788
} else {
88-
std::cerr << "Not found at " << i << std::endl;
89+
// std::cerr << "Not found at " << i << std::endl;
90+
notifications::send("Not found at " + std::to_string(i));
8991
}
9092
}
9193
}

src/services/soundcloud/soundcloud.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <regex>
88
#include <rapidjson/document.h>
99
#include "../../common/Track.h"
10+
#include "../../common/notification.hpp"
1011

1112
class SoundCloud{
1213
public:
@@ -137,7 +138,8 @@ class SoundCloud{
137138
rapidjson::Document document;
138139
document.Parse(j.c_str());
139140
if (document.HasParseError()) {
140-
std::cerr << "JSON parsing error: " << document.GetParseError() << std::endl;
141+
//std::cerr << "JSON parsing error: " << document.GetParseError() << std::endl;
142+
notifications::send("JSON parsing error: " + std::to_string(document.GetParseError()));
141143
}
142144

143145
if (document.HasMember("id") && document["id"].IsInt64()) {
@@ -166,7 +168,8 @@ class SoundCloud{
166168
document.Parse(j.c_str());
167169

168170
if(document.HasParseError()) {
169-
std::cerr << "JSON parsing error: " << document.GetParseError() << std::endl;
171+
// std::cerr << "JSON parsing error: " << document.GetParseError() << std::endl;
172+
notifications::send("JSON parsing error: " + std::to_string(document.GetParseError()));
170173
}
171174

172175
if(document.HasMember("collection") && document["collection"].IsArray()) {

src/storage/localStorage.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <ostream>
1010
#include <vector>
1111
#include <filesystem>
12+
#include "../common/notification.hpp"
1213

1314
void saveData(const std::vector<Track> &recentlyPlayed,
1415
const std::vector<Track> &favorites_tracks) {
@@ -44,7 +45,8 @@ void saveData(const std::vector<Track> &recentlyPlayed,
4445
std::string tracks_file = data_dir + "/tracks.json";
4546
FILE* outFile = fopen(tracks_file.c_str(), "wb");
4647
if (!outFile) {
47-
std::cerr << "Failed to open file for writing" << std::endl;
48+
// std::cerr << "Failed to open file for writing" << std::endl;
49+
notifications::send("Failed to open file for writing");
4850
return;
4951
}
5052

@@ -63,7 +65,8 @@ bool loadData(std::vector<Track> &recentlyPlayed,
6365
std::string tracks_file = data_dir + "/tracks.json";
6466
FILE* inFile = fopen(tracks_file.c_str(), "rb");
6567
if (!inFile) {
66-
std::cerr << "Failed to open tracks file" << std::endl;
68+
// std::cerr << "Failed to open tracks file" << std::endl;
69+
notifications::send("Failed to open tracks file");
6770
return false;
6871
}
6972

@@ -74,7 +77,8 @@ bool loadData(std::vector<Track> &recentlyPlayed,
7477
fclose(inFile);
7578

7679
if (doc.HasParseError()) {
77-
std::cerr << "JSON parse error" << std::endl;
80+
// std::cerr << "JSON parse error" << std::endl;
81+
notifications::send("JSON parse error");
7882
return false;
7983
}
8084

0 commit comments

Comments
 (0)