-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclipboard_manager.sh
executable file
·184 lines (164 loc) · 5.26 KB
/
clipboard_manager.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/bin/bash
# Summary: Clipboard Manager using simple interface, sqlite3 database and xclip with simulates
# The Windows + V functionalilty on Windows 11 devices
# Author: rpd
# Date: 2024-11-11
# Version: 0.1.1
# Tested on Ubuntu 22.04 LTS Pop_OS!
# =============================================================================================
# Configuration
CLIPBOARD_DIR="$HOME/.clipboard_history"
MAX_ENTRIES=25
DB_FILE="$CLIPBOARD_DIR/clipboard.db"
LOCK_FILE="$CLIPBOARD_DIR/clipboard.lock"
# Check and install dependencies
check_dependencies() {
local deps=("zenity" "xclip" "sqlite3")
for dep in "${deps[@]}"; do
if ! command -v "$dep" &> /dev/null; then
zenity --question \
--title="Missing Dependency" \
--text="$dep is not installed. Would you like to install it?" \
--width=300
if [ $? -eq 0 ]; then
if command -v apt &> /dev/null; then
sudo apt-get update && sudo apt-get install -y "$dep"
elif command -v dnf &> /dev/null; then
sudo dnf install -y "$dep"
elif command -v pacman &> /dev/null; then
sudo pacman -S --noconfirm "$dep"
else
zenity --error --text="Could not install $dep. Please install it manually."
exit 1
fi
else
exit 1
fi
fi
done
}
# Initialize clipboard directory and database
init_clipboard() {
mkdir -p "$CLIPBOARD_DIR"
if [ ! -f "$DB_FILE" ]; then
sqlite3 "$DB_FILE" <<EOF
CREATE TABLE clipboard (
id INTEGER PRIMARY KEY AUTOINCREMENT,
content TEXT NOT NULL,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
);
EOF
fi
}
# Add new item to clipboard history
add_to_history() {
local content="$1"
# Escape single quotes for SQL
content="${content//\'/\'\'}"
# Add new entry
sqlite3 "$DB_FILE" "INSERT INTO clipboard (content) VALUES ('$content');"
# Keep only last MAX_ENTRIES
sqlite3 "$DB_FILE" "DELETE FROM clipboard WHERE id NOT IN (SELECT id FROM clipboard ORDER BY timestamp DESC LIMIT $MAX_ENTRIES);"
}
# Monitor clipboard for changes
monitor_clipboard() {
local previous_clipboard=""
while true; do
current_clipboard=$(xclip -selection clipboard -o 2>/dev/null)
if [ "$current_clipboard" != "$previous_clipboard" ] && [ ! -z "$current_clipboard" ]; then
# Use flock to prevent race conditions
(
flock -x 200
add_to_history "$current_clipboard"
) 200>$LOCK_FILE
previous_clipboard="$current_clipboard"
fi
sleep 0.5
done
}
# Show clipboard history GUI
show_history() {
# Get entries from database
local entries=$(sqlite3 "$DB_FILE" "SELECT content, datetime(timestamp, 'localtime') FROM clipboard ORDER BY timestamp DESC;")
if [ -z "$entries" ]; then
zenity --info --text="No clipboard history available." --width=300
return
fi
# Create array of entries for zenity list
local items=()
while IFS='|' read -r content timestamp; do
items+=("[$timestamp] $content")
done < <(echo "$entries")
# Show selection dialog using array
selected=$(zenity --list \
--title="Clipboard History" \
--text="Select an item to copy:" \
--column="Content" \
"${items[@]}" \
--width=800 \
--height=600)
if [ ! -z "$selected" ]; then
# Extract content without timestamp
content=$(echo "$selected" | sed 's/\[[^]]*\] //')
echo -n "$content" | xclip -selection clipboard
zenity --info \
--text="Copied to clipboard!" \
--timeout=2
fi
}
# Main
if [ "$1" = "--show-history" ]; then
show_history
exit 0
fi
check_dependencies
init_clipboard
# Start clipboard monitor in background
monitor_clipboard &
MONITOR_PID=$!
# Create system tray icon (if available)
if command -v yad &> /dev/null; then
yad --notification \
--image="edit-copy" \
--text="Clipboard Manager" \
--command="bash -c 'show_history'" &
TRAY_PID=$!
fi
# Trap SIGTERM and SIGINT
trap 'kill $MONITOR_PID 2>/dev/null; kill $TRAY_PID 2>/dev/null; exit 0' TERM INT
# Main menu
while true; do
action=$(zenity --list \
--title="Clipboard Manager" \
--text="Choose an action:" \
--column="Action" \
"Show Clipboard History" \
"Clear History" \
"Exit" \
--width=300 \
--height=250)
case "$action" in
"Show Clipboard History")
show_history
;;
"Clear History")
zenity --question \
--text="Are you sure you want to clear clipboard history?" \
--width=300
if [ $? -eq 0 ]; then
sqlite3 "$DB_FILE" "DELETE FROM clipboard;"
zenity --info \
--text="Clipboard history cleared!" \
--timeout=2
fi
;;
"Exit")
kill $MONITOR_PID 2>/dev/null
kill $TRAY_PID 2>/dev/null
exit 0
;;
*)
exit 0
;;
esac
done