1+ ;╔═══════════════════════════════════════════════════════════════════════════════╗
2+ ;║ FileIO.pb - version 0.11-alpha ║
3+ ;╟───────────────────────────────────────────────────────────────────────────────╢
4+ ;║ Copyright 2021-2025 Duarte Mendes <duartenm@net.sapo.pt> ║
5+ ;║ ║
6+ ;║ Permission is hereby granted, free of charge, To any person obtaining a copy ║
7+ ;║ of this software And associated documentation files (the "Software"), To deal ║
8+ ;║ in the Software without restriction, including without limitation the rights ║
9+ ;║ To use, copy, modify, merge, publish, distribute, sublicense, And/Or sell ║
10+ ;║ copies of the Software, subject To the following conditions: ║
11+ ;║ ║
12+ ;║ The above copyright notice And this permission notice shall be included in ║
13+ ;║ all copies Or substantial portions of the Software. ║
14+ ;║ ║
15+ ;║ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, ║
16+ ;║ EXPRESS Or IMPLIED, INCLUDING BUT Not LIMITED To THE WARRANTIES ║
17+ ;║ OF MERCHANTABILITY, FITNESS For A PARTICULAR PURPOSE And NONINFRINGEMENT. ║
18+ ;╟───────────────────────────────────────────────────────────────────────────────╢
19+ ;║ > PURPOSE : ║
20+ ;║ Handles File Operations (read/write) for program Notes.C . ║
21+ ;║ ║
22+ ;╚═══════════════════════════════════════════════════════════════════════════════╝
23+
24+
25+ ;Update status bar after i/o operations
26+ Procedure UpdateStatusBar()
27+ Protected Text$ = "Note ID : " + Str(NoteID) + " | Filename : " + DatabaseFile$
28+ StatusBarText(0,0,Text$)
29+ Text$ = "Created : " + FormatDate("%dd/%mm/%yyyy", DateCreated) + " | Last modified : " + FormatDate("%dd/%mm/%yyyy", DateLast)
30+ StatusBarText(0,1,Text$)
31+ EndProcedure
32+
33+
34+ ;Finds the records current/last position (NoteID)
35+ Procedure.l FindMyPosition()
36+
37+ Protected Text4Query$ = "Select NoteID FROM NotesDB"
38+ If DatabaseQuery(#MyDATABASE, Text4Query$)
39+ While NextDatabaseRow(#MyDATABASE)
40+ Protected c.l = GetDatabaseLong(#MyDATABASE, 0)
41+ Wend
42+ FinishDatabaseQuery(#MyDATABASE)
43+ ProcedureReturn c
44+ Else
45+ ProcedureReturn #False
46+ EndIf
47+
48+ EndProcedure
49+
50+
51+ ; Save Tags "#" And "@" into database
52+ Procedure SaveTags(MyTextWithTags$)
53+
54+ ; Find and saves Hashtags '#'
55+ Protected x.l = 0
56+ Repeat
57+ x = FindString(MyTextWithTags$,"#", x + 1)
58+ If x <> 0
59+ Protected ExtractedString$ = Mid(MyTextWithTags$, x, FindString(MyTextWithTags$, " ", x) - x)
60+ CheckDatabaseUpdate(#MyDATABASE, "INSERT INTO HashtagDB (NoteID, Hashtag) VALUES ('" + Str(NoteID) +"', '" + ExtractedString$ + "')")
61+ EndIf
62+ Until x = 0
63+
64+ ; Find and saves Recipients '@'
65+ x = 0
66+ Repeat
67+ x = FindString(MyTextWithTags$,"@", x + 1)
68+ If x <> 0
69+ ExtractedString$ = Mid(MyTextWithTags$, x, FindString(MyTextWithTags$, " ", x) - x)
70+ CheckDatabaseUpdate(#MyDATABASE, "INSERT INTO RecipientDB (NoteID, Recipient) VALUES ('" + Str(NoteID) + "', '" + ExtractedString$ + "')")
71+ EndIf
72+ Until x = 0
73+ EndProcedure
74+
75+
76+ ; Handles menu "Save"
77+ Procedure SaveMyFile()
78+
79+ Protected Text$ = GetScintillaAllText(Scintilla_0) ; Gets the content of Scintilla !
80+ DateLast = Date()
81+
82+ If DatabaseConnected = #True
83+ Protected Modified.b = ScintillaSendMessage(Scintilla_0, #SCI_GETMODIFY) ; Check if text was modified since last save
84+
85+ If Modified > 0
86+ If NoteID > 0
87+ ;SQL insert
88+ Protected Text4Query$ = "UPDATE NotesDB SET Note = '" + Text$
89+ Text4Query$ +"', Format = '" + GetScintillaFormat(Scintilla_0) + "', DateLast = '" + Str(DateLast)
90+ Text4Query$ + "' WHERE NoteID = '" + Str(NoteID)+"'"
91+ CheckDatabaseUpdate(#MyDATABASE, Text4Query$)
92+ ScintillaSendMessage(Scintilla_0, #SCI_SETSAVEPOINT) ;scintilla savepoint notification
93+ UpdateStatusBar()
94+ ; -------------------------->>>>>>>>>>>>>>>>>>>>>>>>>> ToDo : Colocar a rotina de verificar os hashtags [LisIcon versus findstring(Text$)]
95+ Else
96+ DateCreated = Date()
97+ Text4Query$ = "INSERT INTO NotesDB (Note, Format, DateNew, DateLast) VALUES ('"
98+ Text4Query$ + Text$ + "', '" + GetScintillaFormat(Scintilla_0) + "', '" + Str(DateCreated) + "', '"+ Str(DateLast)+ "')"
99+ CheckDatabaseUpdate(#MyDatabase, Text4Query$) ; SQL command execute INSERT NEW
100+ NoteID = FindMyPosition()
101+ ScintillaSendMessage(Scintilla_0, #SCI_SETSAVEPOINT) ;scintilla savepoint notification
102+ SaveTags(Text$)
103+ UpdateStatusBar()
104+ EndIf
105+ EndIf
106+ Else
107+ DatabaseFile$ = SaveFileRequester("New File","noname.sqlite", "sqlite DB (*.sqlite)|*.sqlite|All Files (*.*)|*.*",0)
108+ If DatabaseFile$<>""
109+ Protected Error.l = HandleMyError(CreateFile(#MyFile,DatabaseFile$),"File creation failure!",0)
110+ If Error = #False
111+ CloseFile(#MyFile)
112+
113+ Protected NewError.l = HandleMyError(OpenDatabase(#MyDATABASE, DatabaseFile$, "", "",#PB_Database_SQLite),"DataBase connection failure!",0)
114+ If NewError = #False
115+ ; Creation of a new Tables
116+ CheckDatabaseUpdate(#MyDATABASE, "CREATE TABLE NotesDB (NoteID INTEGER PRIMARY KEY AUTOINCREMENT, Note CHAR, Format CHAR, DateNew INT, DateLast INT)")
117+ CheckDatabaseUpdate(#MyDATABASE, "CREATE TABLE HashtagDB (H_ID INTEGER PRIMARY KEY AUTOINCREMENT, NoteID INT, Hashtag CHAR)")
118+ CheckDatabaseUpdate(#MyDATABASE, "CREATE TABLE RecipientDB (R_ID INTEGER PRIMARY KEY AUTOINCREMENT, NoteID INT, Recipient CHAR)")
119+
120+ ; Saves the current content, if any ...
121+ DateCreated = Date()
122+ Text4Query$ = "INSERT INTO NotesDB (Note, Format, DateNew, DateLast) VALUES ('"
123+ Text4Query$ + Text$ + "', '" + GetScintillaFormat(Scintilla_0) + "', '" + Str(DateCreated) + "', '"+ Str(DateLast)+ "')"
124+ CheckDatabaseUpdate(#MyDatabase, Text4Query$) ; SQL command execute INSERT NEW
125+ ScintillaSendMessage(Scintilla_0, #SCI_SETSAVEPOINT) ;scintilla savepoint notification
126+ NoteID = 1
127+
128+ SaveTags(Text$)
129+ UpdateStatusBar()
130+
131+ DatabaseConnected=#True
132+
133+ MessageRequester("Success!", "File Saved sucessfully!", #PB_MessageRequester_Info)
134+ EndIf
135+ EndIf
136+ EndIf
137+ EndIf
138+ EndProcedure
139+
140+ ;Routine for checking if text was modified and have the option to save it
141+ Procedure.b CheckModifiedAndSave()
142+ Protected Modified.b = ScintillaSendMessage(Scintilla_0, #SCI_GETMODIFY) ; Check if text was modified since last save
143+
144+ If Modified > 0 ; If modified from last save, ask question to save ...
145+ Protected Answer.l = MessageRequester("Question?","Save current Note ?", #PB_MessageRequester_Warning | #PB_MessageRequester_YesNoCancel)
146+
147+ Select Answer
148+ Case #PB_MessageRequester_Cancel
149+ ProcedureReturn #False ; Case CANCEL - the only option that remain the "as is" !
150+ Case #PB_MessageRequester_Yes
151+ SaveMyFile() ; Saves if YES
152+ ProcedureReturn #True
153+ Default ; Case #PB_MessageRequester_No
154+ ProcedureReturn #True
155+ EndSelect
156+ Else
157+ ProcedureReturn #True ; Case not modified
158+ EndIf
159+ EndProcedure
160+
161+
162+ ; Handles menu Open
163+ Procedure OpenMyFile()
164+
165+ Static OldDatabaseFile$
166+
167+ If CheckModifiedAndSave() = #True ; LOAD operations
168+ DatabaseFile$ = OpenFileRequester("Open File","", "sqlite DB (*.sqlite)|*.sqlite|Todos (*.*)|*.*",0)
169+
170+ If DatabaseFile$ <> ""
171+
172+ If DatabaseConnected = #True
173+ HandleMyError(CloseDatabase(#MyDATABASE),"Error closing previous File!",1)
174+ EndIf
175+
176+ Protected Error.l = HandleMyError(OpenDatabase(#MyDATABASE, DatabaseFile$, "", "",#PB_Database_SQLite),"Error opening File!",0)
177+
178+ If Error = #False
179+ DatabaseConnected=#True
180+ OldDatabaseFile$ = DatabaseFile$ ; Keep records of previous file for reconnecting if necessary !
181+ ScintillaSendMessage(Scintilla_0, #SCI_SETREADONLY,0) ; Desprotect any text in Scintilla !
182+ ScintillaSendMessage(Scintilla_0, #SCI_CLEARALL)
183+
184+ If DatabaseQuery(#MyDATABASE, "SELECT * FROM NotesDB")
185+ While NextDatabaseRow(#MyDATABASE)
186+ Protected Text$ = GetDatabaseString(#MyDATABASE, 1)
187+ Protected Format$ = GetDatabaseString(#MyDATABASE,2)
188+ MyScintillaText(Scintilla_0, Text$, Format$, 1)
189+ NoteID = GetDatabaseLong(#MyDATABASE,0)
190+ DateCreated = GetDatabaseLong(#MyDATABASE,3)
191+ DateLast = GetDatabaseLong(#MyDATABASE,4)
192+
193+ Text$ = #CRLF$ + "NoteID [" + Str(NoteID) + "] - Created/Modified : " + FormatDate("%dd/%mm/%yyyy", DateCreated) + " - " + FormatDate("%dd/%mm/%yyyy", DateLast) + #CRLF$
194+ Format$ = "00"
195+
196+ Protected x.l = 3 ; 2 charaters are skiped = Chr(10) + Chr(13)
197+ For x = 3 To Len(Text$)
198+ Format$ + "6"
199+ Next x
200+
201+ MyScintillaText(Scintilla_0, Text$, Format$, 1)
202+ Wend
203+ FinishDatabaseQuery(#MyDATABASE)
204+ EndIf
205+
206+
207+ ContractFoldsScintilla(Scintilla_0)
208+ ScintillaSendMessage(Scintilla_0, #SCI_SETSAVEPOINT)
209+ ScintillaSendMessage(Scintilla_0, #SCI_EMPTYUNDOBUFFER)
210+ ScintillaSendMessage(Scintilla_0, #SCI_SETREADONLY,1)
211+ UpdateStatusBar()
212+
213+ MessageRequester("Success!","File Loaded successfully!", #PB_MessageRequester_Info)
214+ Else
215+ If DatabaseConnected=#True
216+ HandleMyError(OpenDatabase(#MyDATABASE, OldDatabaseFile$, "", "",#PB_Database_SQLite),"Error loading previous File!",1) ;On error tries to reconnect to previous file ...
217+ EndIf
218+ EndIf
219+ EndIf
220+ EndIf
221+ EndProcedure
222+
223+
224+ ; Handles Menu "New"
225+ Procedure NewNoteOrFile()
226+ ScintillaSendMessage(Scintilla_0, #SCI_SETREADONLY,0) ; Desprotect any text in Scintilla !
227+
228+ If CheckModifiedAndSave() = #True
229+ NoteID = 0
230+ ScintillaSendMessage(Scintilla_0, #SCI_CLEARALL)
231+ ScintillaSendMessage(Scintilla_0, #SCI_SETSAVEPOINT) ;scintilla savepoint notification
232+ EndIf
233+ EndProcedure
234+
235+
236+ Procedure CloseMyFile()
237+
238+ If (DatabaseConnected = #True) And (CheckModifiedAndSave() = #True)
239+ HandleMyError(CloseDatabase(#MyDATABASE),"Error closing previous File!",1)
240+ DatabaseConnected = #False
241+ NoteID = 0
242+ DatabaseFile$ = ""
243+ DateCreated = Date()
244+ DateLast = Date()
245+ ScintillaSendMessage(Scintilla_0, #SCI_SETREADONLY,0) ; Desprotect any text in Scintilla !
246+ ScintillaSendMessage(Scintilla_0, #SCI_CLEARALL)
247+ ScintillaSendMessage(Scintilla_0, #SCI_EMPTYUNDOBUFFER)
248+ ScintillaSendMessage(Scintilla_0, #SCI_SETSAVEPOINT) ;scintilla savepoint notification
249+ UpdateStatusBar()
250+ EndIf
251+ EndProcedure
252+
253+ ; IDE Options = PureBasic 6.21 (Windows - x64)
254+ ; CursorPosition = 1
255+ ; Folding = --
256+ ; EnableXP
0 commit comments