use crate::{notification, text};
use glib::{get_user_special_dir, UserDirectory};
use open;
use screenshot_rs;
use screenshot_rs::ScreenshotKind;
use std::path::PathBuf;
use std::{env, fs, path};
use time;
pub fn image(kind: ScreenshotKind) {
let tmp = temp_dir();
let temp = tmp.to_str().unwrap().to_string();
match kind {
ScreenshotKind::Area => screenshot_rs::screenshot_area(temp, true),
ScreenshotKind::Window => screenshot_rs::screenshot_window(temp),
ScreenshotKind::Full => screenshot_rs::screenshot_full(temp),
}
if !tmp.is_file() {
eprintln!("{}", text::message(30));
text::exit();
}
let xdg_pictures = get_user_special_dir(UserDirectory::Pictures).unwrap();
let home = xdg_pictures.to_str().unwrap();
let mut file = String::from(home);
file.push_str("/ShareXin/");
let folder_date = String::from(match time::strftime("%Y-%m", &time::now()) {
Ok(ok) => ok,
Err(_) => {
eprintln!("{}", text::message(25));
text::exit()
}
});
file.push_str(&folder_date);
if fs::create_dir_all(file.clone()).is_err() {
if !path::Path::new(&file).is_dir() {
eprintln!("{}", text::message(30));
}
};
file.push_str("/sharexin-");
let time = String::from(match time::strftime("%Y-%m-%d-%H_%M_%S", &time::now()) {
Ok(ok) => ok,
Err(_) => {
eprintln!("{}", text::message(25));
text::exit()
}
});
file.push_str(&time);
file.push_str(".png");
if fs::copy(tmp.clone(), file).is_err() {
eprintln!("{}", text::message(30));
};
}
pub fn file(file: String) {
let tmp = temp_dir();
if fs::copy(file, tmp.clone()).is_err() {
eprintln!("{}", text::message(30));
text::exit();
};
}
pub fn delete_temp() {
fs::remove_file(temp_dir()).unwrap();
}
pub fn open_temp() {
if open::that(temp_dir()).is_err() {
eprintln!("{}", text::message(19));
notification::error(19);
return;
};
}
pub fn temp_dir() -> PathBuf {
let mut tmp = env::temp_dir();
tmp.push("sharexin.png");
return tmp;
}