2023-05-29 14:39:10 +00:00
|
|
|
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
2023-05-08 22:30:44 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate self_update;
|
|
|
|
|
2022-09-08 18:33:18 +00:00
|
|
|
pub mod app;
|
|
|
|
pub mod config;
|
|
|
|
pub mod program_status;
|
2023-05-08 22:30:44 +00:00
|
|
|
pub mod self_updater;
|
2022-09-08 18:33:18 +00:00
|
|
|
pub mod tray_icon;
|
2023-07-09 22:43:31 +00:00
|
|
|
use crate::config::{Config, IncludeExclude};
|
2022-09-08 18:33:18 +00:00
|
|
|
use crate::program_status::*;
|
2023-05-08 22:30:44 +00:00
|
|
|
use crate::self_updater::try_update;
|
2022-09-08 18:33:18 +00:00
|
|
|
use app::{App, Apps};
|
|
|
|
use discord_rich_presence::{activity, DiscordIpc, DiscordIpcClient};
|
2023-05-29 14:39:10 +00:00
|
|
|
use std::process::exit;
|
2022-09-08 18:33:18 +00:00
|
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
|
|
|
use std::{thread, time};
|
|
|
|
fn main() {
|
2023-05-08 22:30:44 +00:00
|
|
|
match try_update() {
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(e) => println!("Failed to update! {e}"),
|
|
|
|
}
|
2022-09-08 18:33:18 +00:00
|
|
|
let apps = Apps::construct_apps();
|
2023-07-09 22:43:31 +00:00
|
|
|
let mut config = Config::default();
|
|
|
|
|
|
|
|
config.load_from_file();
|
2022-09-08 18:33:18 +00:00
|
|
|
|
|
|
|
let timeout = time::Duration::from_millis(5000);
|
|
|
|
|
|
|
|
let (tray_receiver, _tray) = tray_icon::create_tray();
|
|
|
|
|
|
|
|
let mut prev_project_name = String::new();
|
|
|
|
let mut project_name = String::new();
|
|
|
|
let mut start_time = 0;
|
|
|
|
let mut drp_is_running = false;
|
|
|
|
let mut discord_client: Option<DiscordIpcClient> = None;
|
|
|
|
let mut current_app_option: Option<&App> = None;
|
2023-07-09 22:43:31 +00:00
|
|
|
'main: loop {
|
2022-09-08 18:33:18 +00:00
|
|
|
if let Some(current_app) = current_app_option {
|
|
|
|
if let Some(real_project_name) = is_program_still_running(¤t_app) {
|
2023-07-09 22:43:31 +00:00
|
|
|
project_name = match config.should_list_include_or_exclude {
|
|
|
|
IncludeExclude::Exclude => real_project_name.clone(),
|
|
|
|
IncludeExclude::Include => current_app.default_project_name.clone(),
|
|
|
|
};
|
|
|
|
for listed_word in &config.keywords_list {
|
|
|
|
match &config.should_list_include_or_exclude {
|
|
|
|
&IncludeExclude::Exclude => {
|
|
|
|
if real_project_name.contains(listed_word.as_str()) {
|
|
|
|
if !&config.show_default_when_excluded {
|
|
|
|
continue 'main;
|
|
|
|
}
|
|
|
|
project_name = current_app.default_project_name.clone();
|
|
|
|
} else {
|
|
|
|
project_name = real_project_name.clone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
&IncludeExclude::Include => {
|
|
|
|
if real_project_name.contains(listed_word.as_str()) {
|
|
|
|
project_name = real_project_name.clone();
|
|
|
|
} else {
|
|
|
|
if !&config.show_default_when_excluded {
|
|
|
|
continue 'main;
|
|
|
|
}
|
|
|
|
project_name = current_app.default_project_name.clone();
|
|
|
|
}
|
|
|
|
}
|
2022-09-08 18:33:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//If discord client isn't connected create and conenct it
|
|
|
|
if !drp_is_running {
|
|
|
|
//make a new client with current_app ID
|
|
|
|
discord_client =
|
|
|
|
match DiscordIpcClient::new(¤t_app.drp_client_id.as_str()) {
|
|
|
|
Ok(ok) => Some(ok),
|
|
|
|
Err(_err) => None,
|
|
|
|
};
|
|
|
|
if let Some(dc_client) = discord_client.as_mut() {
|
|
|
|
match dc_client.connect() {
|
|
|
|
Ok(_ok) => drp_is_running = true,
|
|
|
|
Err(_err) => drp_is_running = false,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
//only if new app is made refresh the start time
|
|
|
|
start_time = SystemTime::now()
|
|
|
|
.duration_since(UNIX_EPOCH)
|
|
|
|
.expect("time went backwards")
|
|
|
|
.as_secs();
|
|
|
|
}
|
|
|
|
//if project name changed, update it
|
|
|
|
if prev_project_name != project_name {
|
|
|
|
prev_project_name = project_name.clone();
|
|
|
|
let details = format!("Working on {}", &project_name.clone());
|
|
|
|
//update activity
|
2023-01-29 16:17:04 +00:00
|
|
|
|
|
|
|
let mut activity = activity::Activity::new()
|
2022-09-08 18:33:18 +00:00
|
|
|
.details(&details.as_str())
|
|
|
|
.assets(
|
|
|
|
activity::Assets::new().large_image(current_app.drp_client_id.as_str()),
|
|
|
|
)
|
|
|
|
.timestamps(activity::Timestamps::new().start(start_time as i64));
|
2023-01-29 16:17:04 +00:00
|
|
|
let state;
|
|
|
|
if !config.hide_portfolio_row {
|
|
|
|
state = format!("Portfolio: {}", &config.portfolio_link);
|
|
|
|
activity = activity.state(state.as_str());
|
|
|
|
}
|
2022-09-08 18:33:18 +00:00
|
|
|
//if discord client exists, update status
|
|
|
|
if let Some(dc) = discord_client.as_mut() {
|
|
|
|
match dc.set_activity(activity) {
|
2023-07-09 22:43:31 +00:00
|
|
|
Ok(_) => (),
|
|
|
|
Err(e) => {
|
|
|
|
dbg!(e);
|
|
|
|
}
|
2022-09-08 18:33:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//if program no longer running
|
|
|
|
else {
|
|
|
|
current_app_option = None;
|
|
|
|
prev_project_name = "".to_string();
|
|
|
|
}
|
|
|
|
//respond to tray icon messages
|
|
|
|
match tray_receiver.try_recv() {
|
|
|
|
Ok(msg) => match msg {
|
2023-07-09 22:43:31 +00:00
|
|
|
tray_icon::Message::AddProjectToList => {
|
|
|
|
if let Some(real_project_name) = is_program_still_running(¤t_app) {
|
|
|
|
config.add_project(&real_project_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tray_icon::Message::RemoveProjectFromList => {
|
|
|
|
if let Some(real_project_name) = is_program_still_running(¤t_app) {
|
|
|
|
config.remove_project(&real_project_name);
|
2022-09-08 18:33:18 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-29 14:39:10 +00:00
|
|
|
tray_icon::Message::Quit => {
|
|
|
|
println!("qiuitting!");
|
|
|
|
exit(0)
|
|
|
|
}
|
2023-07-09 22:43:31 +00:00
|
|
|
tray_icon::Message::OpenOptionsFile => {
|
|
|
|
let _ = std::process::Command::new("crp_config.toml").spawn();
|
|
|
|
}
|
2022-09-08 18:33:18 +00:00
|
|
|
},
|
|
|
|
Err(_err) => (),
|
|
|
|
}
|
|
|
|
} else {
|
2023-05-29 14:39:10 +00:00
|
|
|
//respond to tray icon messages
|
|
|
|
match tray_receiver.try_recv() {
|
|
|
|
Ok(msg) => match msg {
|
|
|
|
tray_icon::Message::Quit => exit(0),
|
2023-07-09 22:43:31 +00:00
|
|
|
_ => (),
|
2023-05-29 14:39:10 +00:00
|
|
|
},
|
|
|
|
Err(_err) => (),
|
|
|
|
}
|
2022-09-08 18:33:18 +00:00
|
|
|
//If nothing is running try to find it
|
|
|
|
if let Some(proj_name) = get_running_program(&apps) {
|
|
|
|
let temp_curr_app: &App;
|
|
|
|
(temp_curr_app, project_name) = proj_name;
|
|
|
|
current_app_option = Some(temp_curr_app);
|
|
|
|
} else {
|
|
|
|
current_app_option = None;
|
|
|
|
}
|
|
|
|
if drp_is_running {
|
|
|
|
match discord_client
|
|
|
|
.as_mut()
|
|
|
|
.expect("Somehow it dissapeared?")
|
|
|
|
.close()
|
|
|
|
{
|
|
|
|
Ok(_ok) => drp_is_running = false,
|
|
|
|
Err(_err) => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
thread::sleep(timeout);
|
|
|
|
}
|
|
|
|
}
|