-
Notifications
You must be signed in to change notification settings - Fork 0
/
note_app.c
329 lines (287 loc) · 10.5 KB
/
note_app.c
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>
// Structure to hold notes and tasks
typedef struct Note {
int id;
char title[100];
char content[500];
char category[50];
time_t created_at;
time_t updated_at;
struct Note *next;
} Note;
typedef struct Reminder {
Note *note;
time_t reminder_at;
} Reminder;
// Function prototypes
Note *create_note(char *title, char *content, char *category);
void edit_note(Note *note, char *title, char *content, char *category);
void delete_note(Note *note);
void search_notes(Note *head, char *keyword);
void *reminder_thread(void *arg);
void set_reminder(Note *note, time_t reminder_at);
void display_notes(Note *head);
void sync_with_gdrive(Note *note);
int main() {
Note *head = NULL;
// Add code to handle user input and call the relevant functions
int choice;
char title[100], content[500], category[50], keyword[100];
int id;
do {
printf("Note-taking and Task-management Application\n");
printf("1. Create a note\n");
printf("2. Edit a note\n");
printf("3. Delete a note\n");
printf("4. Search notes\n");
printf("5. Set a reminder\n");
printf("6. Display all notes\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar(); // To consume the newline character after reading choice
switch (choice) {
case 1:
printf("Enter the title: ");
fgets(title, sizeof(title), stdin);
title[strcspn(title, "\n")] = '\0'; // Remove the newline character
printf("Enter the content: ");
fgets(content, sizeof(content), stdin);
content[strcspn(content, "\n")] = '\0'; // Remove the newline character
printf("Enter the category: ");
fgets(category, sizeof(category), stdin);
category[strcspn(category, "\n")] = '\0'; // Remove the newline character
Note *new_note = create_note(title, content, category);
if (new_note) {
new_note->next = head;
head = new_note;
}
break;
case 2:
printf("Enter the note ID to edit: ");
scanf("%d", &id);
getchar();
Note *note_to_edit = NULL;
for (Note *current = head; current != NULL; current = current->next) {
if (current->id == id) {
note_to_edit = current;
break;
}
}
if (note_to_edit) {
printf("Enter the new title: ");
fgets(title, sizeof(title), stdin);
title[strcspn(title, "\n")] = '\0'; // Remove the newline character
printf("Enter the new content: ");
fgets(content, sizeof(content), stdin);
content[strcspn(content, "\n")] = '\0'; // Remove the newline character
printf("Enter the new category: ");
fgets(category, sizeof(category), stdin);
category[strcspn(category, "\n")] = '\0'; // Remove the newline character
edit_note(note_to_edit, title, content, category);
} else {
printf("Note not found.\n");
}
break;
case 3:
printf("Enter the note ID to delete: ");
scanf("%d", &id);
getchar();
Note *prev = NULL;
Note *note_to_delete = head;
while (note_to_delete != NULL) {
if (note_to_delete->id == id) {
break;
}
prev = note_to_delete;
note_to_delete = note_to_delete->next;
}
if (note_to_delete) {
if (prev == NULL) {
head = note_to_delete->next;
} else {
prev->next = note_to_delete->next;
}
delete_note(note_to_delete);
printf("Note with ID %d deleted.\n", id);
} else {
printf("Note not found.\n");
}
break;
case 4:
printf("Enter the keyword to search: ");
fgets(keyword, sizeof(keyword), stdin);
keyword[strcspn(keyword, "\n")] = '\0'; // Remove the newline character
search_notes(head, keyword);
break;
case 5:
printf("Enter the note ID to set a reminder for: ");
scanf("%d", &id);
getchar();
Note *note_to_remind = NULL;
for (Note *current = head; current != NULL; current = current->next) {
if (current->id == id) {
note_to_remind = current;
break;
}
}
if (note_to_remind) {
time_t reminder_time;
printf("Enter the reminder time in seconds from now: ");
scanf("%ld", &reminder_time);
getchar();
set_reminder(note_to_remind, time(NULL) + reminder_time);
} else {
printf("Note not found.\n");
}
break;
case 6:
display_notes(head);
break;
case 0:
printf("Exiting...\n");
break;
default:
printf("Invalid choice.\n");
}
} while (choice != 0);
// Free allocated memory for notes before exiting
while (head != NULL) {
Note *temp = head;
head = head->next;
delete_note(temp);
}
return 0;
}
Note *create_note(char *title, char *content, char *category) {
Note *new_note = (Note *) malloc(sizeof(Note));
if (!new_note) {
printf("Error allocating memory for new note.\n");
return NULL;
}
new_note->id = rand() % 1000000;
strcpy(new_note->title, title);
strcpy(new_note->content, content);
strcpy(new_note->category, category);
new_note->created_at = time(NULL);
new_note->updated_at = time(NULL);
new_note->next = NULL;
// Create a text file with the note's details
char file_path[100];
sprintf(file_path, "note_%d.txt", new_note->id);
FILE *file = fopen(file_path, "w");
if (file) {
fprintf(file, "Title: %s\n", title);
fprintf(file, "Content: %s\n", content);
fprintf(file, "Category: %s\n", category);
fprintf(file, "Created at: %s", ctime(&new_note->created_at));
fprintf(file, "Updated at: %s", ctime(&new_note->updated_at));
fclose(file);
// Sync with Google Drive
sync_with_gdrive(new_note);
} else {
printf("Error creating file for note.\n");
}
return new_note;
}
void edit_note(Note *note, char *title, char *content, char *category) {
strcpy(note->title, title);
strcpy(note->content, content);
strcpy(note->category, category);
note->updated_at = time(NULL);
// Update the text file with the note's details
char file_path[100];
sprintf(file_path, "note_%d.txt", note->id);
FILE *file = fopen(file_path, "w");
if (file) {
fprintf(file, "Title: %s\n", title);
fprintf(file, "Content: %s\n", content);
fprintf(file, "Category: %s\n", category);
fprintf(file, "Created at: %s", ctime(¬e->created_at));
fprintf(file, "Updated at: %s", ctime(¬e->updated_at));
fclose(file);
sync_with_gdrive(note);
} else {
printf("Error updating file for note.\n");
}
}
void delete_note(Note *note) {
// Delete the text file for the note
char file_path[100];
sprintf(file_path, "note_%d.txt", note->id);
if (remove(file_path) != 0) {
printf("Error deleting file for note.\n");
}
free(note);
}
void search_notes(Note *head, char *keyword) {
Note *current = head;
int count = 0;
printf("Notes containing keyword \"%s\":\n", keyword);
while (current != NULL) {
if (strstr(current->title, keyword) || strstr(current->content, keyword) || strstr(current->category, keyword)) {
count++;
printf("\nNote ID: %d\n", current->id);
printf("Title: %s\n", current->title);
printf("Content: %s\n", current->content);
printf("Category: %s\n", current->category);
printf("Created at: %s", ctime(¤t->created_at));
printf("Updated at: %s\n", ctime(¤t->updated_at));
}
current = current->next;
}
if (count == 0) {
printf("No notes found containing the keyword \"%s\".\n", keyword);
}
}
void *reminder_thread(void *arg) {
Reminder *reminder = (Reminder *)arg;
time_t current_time;
while (1) {
current_time = time(NULL);
if (current_time >= reminder->reminder_at) {
printf("\n[REMINDER] Note ID: %d, Title: %s\n", reminder->note->id, reminder->note->title);
free(reminder);
break;
}
sleep(1);
}
pthread_exit(NULL);
}
void set_reminder(Note *note, time_t reminder_at) {
Reminder *reminder = (Reminder *)malloc(sizeof(Reminder));
reminder->note = note;
reminder->reminder_at = reminder_at;
pthread_t reminder_tid;
pthread_create(&reminder_tid, NULL, reminder_thread, (void *)reminder);
pthread_detach(reminder_tid);
}
void display_notes(Note *head) {
Note *current = head;
if (current == NULL) {
printf("No notes found.\n");
return;
}
printf("All notes:\n");
while (current != NULL) {
printf("\nNote ID: %d\n", current->id);
printf("Title: %s\n", current->title);
printf("Content: %s\n", current->content);
printf("Category: %s\n", current->category);
printf("Created at: %s", ctime(¤t->created_at));
printf("Updated at: %s\n", ctime(¤t->updated_at));
current = current->next;
}
}
void sync_with_gdrive(Note *note) {
char file_path[500];
sprintf(file_path, "note_%d.txt", note->id);
char command[500];
sprintf(command, "python3 gdrive.py upload %.200s %.200s", file_path, file_path);
system(command);
}