├── .gitignore ├── LICENSE ├── README.md ├── meson.build ├── meson_options.txt ├── res ├── com.bjareholt.johan.SimpleDiary.desktop.in ├── com.bjareholt.johan.SimpleDiary.metainfo.xml.in ├── gresources.xml ├── icons │ ├── logo.svg │ └── meson.build ├── meson.build └── ui │ ├── entry_browser.ui │ ├── entry_delete_dialog.ui │ ├── entry_edit.ui │ ├── entry_list.ui │ ├── entry_listing.ui │ ├── entry_name_dialog.ui │ ├── entry_rename_dialog.ui │ ├── entry_view.ui │ ├── image_picker.ui │ ├── meson.build │ ├── settings.ui │ └── window.ui ├── src ├── entry.c ├── entry.h ├── headerbuttons.c ├── headerbuttons.h ├── main.c ├── meson.build ├── notification.c ├── notification.h ├── settings.c ├── settings.h ├── utils.c ├── utils.h ├── widgets │ ├── entry_browser.c │ ├── entry_browser.h │ ├── entry_delete_dialog.c │ ├── entry_delete_dialog.h │ ├── entry_edit.c │ ├── entry_edit.h │ ├── entry_list.c │ ├── entry_list.h │ ├── entry_listing.c │ ├── entry_listing.h │ ├── entry_name_dialog.c │ ├── entry_name_dialog.h │ ├── entry_rename_dialog.c │ ├── entry_rename_dialog.h │ ├── entry_view.c │ ├── entry_view.h │ ├── image_picker.c │ ├── image_picker.h │ ├── settings_view.c │ └── settings_view.h ├── window.c └── window.h ├── test └── meson.build └── tools └── book-generator └── scripts └── md2tex.py /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | build-dir 3 | *.ui~ 4 | .flatpak-builder 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Simple Diary 2 | ============ 3 | 4 | Simple and lightweight diary app. 5 | 6 | ### Installation 7 | 8 | 9 | 10 | 11 | 16 | 17 | 18 | 19 | 23 | 24 |
Flathub 12 | 13 | Get it on flathub 14 | 15 |
Archlinux AUR 20 | simple-diary-gtk, 21 | simple-diary-gtk-git 22 |
25 | 26 | ### Features 27 | - Saves entries in standard markdown 28 | - Adding images to your entries 29 | - Scales on desktops, laptops, tablets and phones 30 | - Dark mode 31 | 32 | ### Dependencies 33 | - meson 34 | - GTK4 35 | - gtkmdview 36 | - gtkmdeditor 37 | - md4c 38 | 39 | ### Building 40 | 41 | First install dependencies listed above 42 | 43 | Secondly run the following: meson build && ninja -C build 44 | 45 | Executable will be built at build/src/simple-diary 46 | 47 | ### Screenshots 48 | 49 |
50 | 51 | 52 |
53 | -------------------------------------------------------------------------------- /meson.build: -------------------------------------------------------------------------------- 1 | project('simple-diary', 'c', 2 | meson_version: '>=0.49', 3 | default_options: ['warning_level=1', 'werror=true']) 4 | 5 | gnome = import('gnome') 6 | 7 | gtk_dep = dependency('gtk4', version : '>=4.10') 8 | adwaita_dep = dependency('libadwaita-1', version : '>=1.5') 9 | gdkpixbuf_dep = dependency('gdk-pixbuf-2.0', version : '>=2.0') 10 | gtkmdview_dep = dependency('gtkmdview') 11 | gtkmdeditor_dep = dependency('gtkmdeditor') 12 | 13 | add_global_link_arguments(['-Wall'], language: 'c') 14 | 15 | app_id = 'com.bjareholt.johan.SimpleDiary' 16 | summary = 'Simple and lightweight diary app' 17 | name = 'Simple Diary' 18 | 19 | subdir('res') 20 | subdir('src') 21 | 22 | if get_option('test').enabled() 23 | subdir('test') 24 | endif 25 | -------------------------------------------------------------------------------- /meson_options.txt: -------------------------------------------------------------------------------- 1 | option('test', type : 'feature', value : 'disabled', description : 'Enable tests') 2 | -------------------------------------------------------------------------------- /res/com.bjareholt.johan.SimpleDiary.desktop.in: -------------------------------------------------------------------------------- 1 | [Desktop Entry] 2 | Name=Simple Diary 3 | Comment=@summary@ 4 | GenericName=@name@ 5 | Exec=simple-diary 6 | # Translators: Do NOT translate or transliterate this text (this is an icon file name)! 7 | Icon=@app_id@ 8 | Type=Application 9 | StartupNotify=true 10 | X-GNOME-UsesNotifications=true 11 | Categories=GNOME;GTK;Office 12 | # Translators: Search terms to find this application. Do NOT translate or localize the semicolons! The list MUST also end with a semicolon! 13 | Keywords=Diary;Journal;GTK;Log;Personal; 14 | # Translators: Do NOT translate or transliterate this text (these are enum types)! 15 | X-Purism-FormFactor=Workstation;Mobile; 16 | -------------------------------------------------------------------------------- /res/com.bjareholt.johan.SimpleDiary.metainfo.xml.in: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | @app_id@ 5 | @name@ 6 | @summary@ 7 | https://github.com/johan-bjareholt/simple-diary-gtk 8 | https://github.com/johan-bjareholt/simple-diary-gtk/issues 9 | 10 | 11 | 12 |

Designed to be easy to use and be out of the way, to let you focus on documenting and reflecting over your daily life.

13 |

A few notable features are:

14 |
    15 |
  • Saves entries in standard markdown
  • 16 |
  • Adding images to your entries
  • 17 |
  • Scales on desktops, laptops, tablets and phones
  • 18 |
  • Dark mode
  • 19 |
20 |
21 | 22 | 23 | Office 24 | 25 | 26 | @app_id@.desktop 27 | 28 | 29 | 30 | View of the list of diary entries 31 | https://johan.bjareholt.com/img/projects/simple-diary/entry_view.png 32 | 33 | 34 | View of the diary entry editor 35 | https://johan.bjareholt.com/img/projects/simple-diary/entry_edit.png 36 | 37 | 38 | 39 | 40 | pointing 41 | keyboard 42 | tablet 43 | touch 44 | 360 45 | 46 | 47 | 48 | Johan Bjäreholt 49 | 50 | 51 | johan _AT_ bjareho.lt 52 | GPL-3.0+ 53 | MIT 54 | 55 | AppMenu 56 | HighContrast 57 | ModernToolkit 58 | 59 | 60 | 61 | 62 | 63 | 64 |
    65 |
  • Add markdown highlighing to editor
  • 66 |
  • Fix crash that could occur when closing an error dialog from the image picker
  • 67 |
68 |
69 |
70 | 71 | 72 | 73 |
    74 |
  • Fix appstream metainfo issues
  • 75 |
76 |
77 |
78 | 79 | 80 | 81 |
    82 |
  • Update flatpak runtime to Gnome 47
  • 83 |
  • Use more Adwaita widgets
  • 84 |
85 |
86 |
87 | 88 | 89 | 90 |
    91 |
  • Flatpak no longer needs read/write access to the whole home folder
  • 92 |
93 |
94 |
95 | 96 | 97 | 98 |
    99 |
  • Minor UI improvements
  • 100 |
  • Update flatpak runtime to Gnome 45
  • 101 |
  • Rewrite dialogs that use code deprecated in GTK 4.10
  • 102 |
103 |
104 |
105 | 106 | 107 | 108 |
    109 |
  • Fix crash when creating new entry
  • 110 |
111 |
112 |
113 | 114 | 115 | 116 |
    117 |
  • Make flatpak use XDG dirs, to allow better security in the future
  • 118 |
  • Minor UI improvements
  • 119 |
120 |
121 |
122 | 123 | 124 |
    125 |
  • New "new entry" dialog with date selection
  • 126 |
  • Improve dark mode setting
  • 127 |
  • Add "About" dialog
  • 128 |
129 |
130 |
131 | 132 | 133 |
    134 |
  • Improved rename and delete dialogs
  • 135 |
  • Improved application metainfo
  • 136 |
137 |
138 |
139 | 140 | 141 |
    142 |
  • Improved settings view
  • 143 |
  • Change all icons to be symbolic for consistency
  • 144 |
  • Start using libadwaita 1.0 and utilize it more
  • 145 |
146 |
147 |
148 | 149 | 150 |
    151 |
  • Migrated from GTK3 to GTK4
  • 152 |
  • Migrated from libhandy to libadwaita
  • 153 |
  • Migrated from GtkWebKit to gtkmdview
  • 154 |
  • Rewrote image picker
  • 155 |
156 |
157 |
158 | 159 | 160 |
    161 |
  • Improved UI for desktop
  • 162 |
  • Lots of bug fixes
  • 163 |
164 |
165 |
166 | 167 | 168 |
    169 |
  • New icon
  • 170 |
  • Improved dark mode
  • 171 |
172 |
173 |
174 | 175 | 176 |
    177 |
  • Add SettingsView
  • 178 |
  • Add DarkMode
  • 179 |
180 |
181 |
182 | 183 | 184 |

Fix bug not properly deleting photos when deleting diary.

185 |
186 |
187 | 188 | 189 |

Improve interface for small formfactor devices.

190 |
191 |
192 | 193 |
194 |
195 | -------------------------------------------------------------------------------- /res/gresources.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ui/window.ui 5 | ui/entry_browser.ui 6 | ui/entry_list.ui 7 | ui/entry_listing.ui 8 | ui/entry_edit.ui 9 | ui/entry_view.ui 10 | ui/entry_name_dialog.ui 11 | ui/entry_rename_dialog.ui 12 | ui/entry_delete_dialog.ui 13 | ui/image_picker.ui 14 | ui/settings.ui 15 | 16 | icons/logo.svg 17 | 18 | 19 | -------------------------------------------------------------------------------- /res/icons/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 17 | 19 | 26 | 34 | 38 | 39 | 40 | 67 | 69 | 70 | 72 | image/svg+xml 73 | 75 | 76 | 77 | 78 | 83 | 92 | 101 | 110 | 111 | 117 | 121 | 125 | 126 | 131 | 135 | 136 | 141 | 145 | 149 | 150 | 155 | 162 | 169 | 173 | 177 | 181 | 185 | 189 | 193 | 197 | 201 | 205 | 209 | 213 | 217 | 221 | 225 | 229 | 233 | 234 | 235 | -------------------------------------------------------------------------------- /res/icons/meson.build: -------------------------------------------------------------------------------- 1 | logo = 'logo.svg' 2 | 3 | scalable_dir = 'hicolor' / 'scalable' / 'apps' 4 | install_data( 5 | logo, 6 | install_dir: get_option('datadir') / 'icons' / scalable_dir, 7 | rename: '@0@.svg'.format(app_id) 8 | ) 9 | 10 | symbolic_dir = join_paths('hicolor', 'symbolic', 'apps') 11 | install_data( 12 | logo, 13 | install_dir: get_option('datadir') / 'icons' / symbolic_dir, 14 | rename: '@0@-symbolic.svg'.format(app_id) 15 | ) 16 | -------------------------------------------------------------------------------- /res/meson.build: -------------------------------------------------------------------------------- 1 | 2 | gresources = gnome.compile_resources( 3 | 'gresources', 4 | 'gresources.xml', 5 | ) 6 | 7 | conf_data = configuration_data() 8 | conf_data.set('app_id', app_id) 9 | conf_data.set('summary', summary) 10 | conf_data.set('name', name) 11 | 12 | desktop_file = configure_file( 13 | input: files(app_id + '.desktop.in'), 14 | output: app_id + '.desktop', 15 | configuration: conf_data 16 | ) 17 | 18 | desktop_file_validate = find_program('desktop-file-validate', required: false) 19 | if desktop_file_validate.found() 20 | test( 21 | 'validate-desktop', 22 | desktop_file_validate, 23 | args: [ 24 | desktop_file 25 | ] 26 | ) 27 | endif 28 | 29 | install_data( 30 | desktop_file, 31 | install_dir: get_option('prefix') / 'share/applications' 32 | ) 33 | 34 | appstream_file = configure_file( 35 | input: app_id + '.metainfo.xml.in', 36 | output: app_id + '.metainfo.xml', 37 | configuration: conf_data, 38 | ) 39 | 40 | 41 | appstream_util = find_program('appstream-util', required: false) 42 | if appstream_util.found() 43 | test('Validate appstream file', appstream_util, 44 | args: ['validate', appstream_file] 45 | ) 46 | endif 47 | 48 | install_data( 49 | appstream_file, 50 | install_dir: get_option('datadir') / 'metainfo', 51 | ) 52 | 53 | subdir('icons') 54 | -------------------------------------------------------------------------------- /res/ui/entry_browser.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 42 | 43 | -------------------------------------------------------------------------------- /res/ui/entry_delete_dialog.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 50 | 51 | -------------------------------------------------------------------------------- /res/ui/entry_edit.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 31 | 32 | -------------------------------------------------------------------------------- /res/ui/entry_list.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16 | 17 | -------------------------------------------------------------------------------- /res/ui/entry_listing.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16 | 17 | -------------------------------------------------------------------------------- /res/ui/entry_name_dialog.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 90 | 91 | -------------------------------------------------------------------------------- /res/ui/entry_rename_dialog.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 62 | 63 | -------------------------------------------------------------------------------- /res/ui/entry_view.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 83 | 84 | -------------------------------------------------------------------------------- /res/ui/image_picker.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 90 | 91 | -------------------------------------------------------------------------------- /res/ui/meson.build: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/johan-bjareholt/simple-diary-gtk/0e5c2296c041da9b2bc01bdb18f1faf4a15ba7ee/res/ui/meson.build -------------------------------------------------------------------------------- /res/ui/settings.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 72 | 73 | -------------------------------------------------------------------------------- /res/ui/window.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 52 | 53 | -------------------------------------------------------------------------------- /src/entry.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "entry.h" 7 | #include "utils.h" 8 | 9 | struct _Entry 10 | { 11 | GObject parent; 12 | 13 | gchar *folder; 14 | gchar *filename; 15 | }; 16 | 17 | G_DEFINE_TYPE (Entry, entry, G_TYPE_OBJECT); 18 | 19 | typedef enum 20 | { 21 | SIGNAL_DELETED = 1, 22 | SIGNAL_LAST, 23 | } EntrySignal; 24 | 25 | static guint obj_signals[SIGNAL_LAST] = { 0, }; 26 | 27 | typedef enum 28 | { 29 | PROP_FOLDER = 1, 30 | PROP_FILENAME, 31 | PROP_BASENAME, 32 | PROP_PATH, 33 | NUM_PROPS, 34 | } EntryProperties; 35 | 36 | static GParamSpec *obj_properties[NUM_PROPS] = { NULL, }; 37 | 38 | gchar * 39 | filename_to_basename (gchar *filename) 40 | { 41 | gchar *basename; 42 | gchar *last_dot; 43 | 44 | basename = g_path_get_basename (filename); 45 | last_dot = strrchr (basename, '.'); 46 | last_dot[0] = '\0'; 47 | 48 | return basename; 49 | } 50 | 51 | gboolean 52 | entry_rename_file (Entry *self, gchar *new_name) 53 | { 54 | gchar *old_file_path; 55 | gchar *new_file_path; 56 | 57 | gchar *old_basename; 58 | gchar *new_basename; 59 | 60 | g_autofree char* input_text = NULL; 61 | g_autofree char* output_text = NULL; 62 | 63 | gchar *old_photos_path_full = NULL; 64 | gchar *new_photos_path_full = NULL; 65 | 66 | gchar *old_photos_path_short = NULL; 67 | gchar *new_photos_path_short = NULL; 68 | 69 | /* TODO: Validate that it ends with .md */ 70 | 71 | /* get basenames */ 72 | old_basename = filename_to_basename (self->filename); 73 | new_basename = filename_to_basename (new_name); 74 | 75 | /* move md file */ 76 | old_file_path = g_strdup_printf ("%s/%s", self->folder, self->filename); 77 | new_file_path = g_strdup_printf ("%s/%s", self->folder, new_name); 78 | if (rename (old_file_path, new_file_path) < 0) { 79 | /* TODO: Exit gracefully, show popup */ 80 | g_printerr ("Failed to rename diary file '%s': %s\n", old_basename, strerror(errno)); 81 | exit(EXIT_FAILURE); 82 | } 83 | g_free (old_file_path); 84 | g_free (new_file_path); 85 | 86 | /* apply new name */ 87 | g_object_set (self, "filename", new_name, NULL); 88 | 89 | /* move photos folder */ 90 | old_photos_path_full = g_strdup_printf ("%s/photos/%s", self->folder, old_basename); 91 | new_photos_path_full = g_strdup_printf ("%s/photos/%s", self->folder, new_basename); 92 | if (rename (old_photos_path_full, new_photos_path_full) < 0) { 93 | // TODO: Exit gracefully, show popup 94 | if (errno == ENOENT) { 95 | goto out; 96 | } 97 | g_printerr ("Failed to rename photos folder for diary '%s': %s\n", old_basename, strerror(errno)); 98 | exit(EXIT_FAILURE); 99 | } 100 | 101 | /* Change photo links to new folder */ 102 | old_photos_path_short = g_strdup_printf ("photos/%s", old_basename); 103 | new_photos_path_short = g_strdup_printf ("photos/%s", new_basename); 104 | GError *err = NULL; 105 | input_text = entry_read (self, &err); 106 | if (input_text == NULL) { 107 | // TODO: Exit gracefully, show popup 108 | g_printerr ("Failed to read diary entry '%s': %s\n", old_basename, err->message); 109 | exit(EXIT_FAILURE); 110 | } 111 | char **split = g_strsplit(input_text, old_photos_path_short, -1); 112 | output_text = g_strjoinv(new_photos_path_short, split); 113 | g_strfreev(split); 114 | if (!entry_write (self, output_text, NULL)) { 115 | // TODO: Exit gracefully, show popup 116 | g_printerr ("Failed to write diary entry '%s': %s\n", old_basename, err->message); 117 | exit(EXIT_FAILURE); 118 | } 119 | 120 | out: 121 | g_free (old_photos_path_short); 122 | g_free (new_photos_path_short); 123 | 124 | g_free (old_photos_path_full); 125 | g_free (new_photos_path_full); 126 | 127 | g_free (old_basename); 128 | g_free (new_basename); 129 | 130 | return TRUE; 131 | } 132 | 133 | static void 134 | entry_set_property (GObject *object, 135 | guint property_id, 136 | const GValue *value, 137 | GParamSpec *pspec) 138 | { 139 | Entry *self = DIARY_ENTRY (object); 140 | 141 | switch ((EntryProperties) property_id) { 142 | case PROP_FOLDER: 143 | if (self->folder != NULL) 144 | g_free (self->folder); 145 | self->folder = g_strdup(g_value_get_string(value)); 146 | break; 147 | case PROP_FILENAME: 148 | if (self->filename != NULL) 149 | g_free (self->filename); 150 | self->filename = g_strdup(g_value_get_string(value)); 151 | break; 152 | default: 153 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); 154 | break; 155 | } 156 | } 157 | 158 | static void 159 | entry_get_property (GObject *object, 160 | guint property_id, 161 | GValue *value, 162 | GParamSpec *pspec) 163 | { 164 | Entry *self = DIARY_ENTRY (object); 165 | 166 | switch ((EntryProperties) property_id) 167 | { 168 | case PROP_FOLDER: 169 | g_value_set_string (value, self->folder); 170 | break; 171 | case PROP_FILENAME: 172 | g_value_set_string (value, self->filename); 173 | break; 174 | case PROP_BASENAME: { 175 | gchar *basename = filename_to_basename (self->filename); 176 | g_value_set_string (value, basename); 177 | g_free (basename); 178 | break; 179 | } 180 | case PROP_PATH: { 181 | gchar *path; 182 | path = g_strdup_printf ("%s/%s", self->folder, self->filename); 183 | g_value_set_string (value, path); 184 | g_free (path); 185 | break; 186 | } 187 | default: 188 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); 189 | break; 190 | } 191 | } 192 | 193 | gchar * 194 | entry_read(Entry *self, GError **err) 195 | { 196 | gchar *filepath; 197 | gchar *body; 198 | gsize length; 199 | GError *err_local = NULL; 200 | 201 | g_object_get (self, "filepath", &filepath, NULL); 202 | if (!g_file_get_contents (filepath, &body, &length, &err_local)) { 203 | if (err_local->code == G_FILE_ERROR_NOENT) { 204 | body = g_strdup (""); 205 | g_clear_error (&err_local); 206 | goto cleanup; 207 | } 208 | g_error ("failed to read entry: %s", err_local->message); 209 | } 210 | 211 | cleanup: 212 | if (err_local != NULL && err != NULL) { 213 | *err = err_local; 214 | } 215 | g_free (filepath); 216 | return body; 217 | } 218 | 219 | gboolean 220 | entry_write (Entry *self, gchar *text, GError **err) 221 | { 222 | gchar *filepath; 223 | gboolean ret; 224 | 225 | g_object_get (self, "filepath", &filepath, NULL); 226 | 227 | ret = g_file_set_contents (filepath, text, -1, err); 228 | 229 | g_print ("saved %s\n", filepath); 230 | 231 | g_free (filepath); 232 | 233 | return ret; 234 | } 235 | 236 | gboolean 237 | entry_delete (Entry *self) 238 | { 239 | gboolean ret = FALSE; 240 | GFile *file; 241 | GDir *dir = NULL; 242 | gchar *filepath = NULL; 243 | const gchar *filename = NULL; 244 | gchar *basename = NULL; 245 | gchar *photos_path = NULL; 246 | GError *err = NULL; 247 | 248 | g_object_get (self, "filepath", &filepath, NULL); 249 | file = g_file_new_for_path (filepath); 250 | 251 | g_print ("deleting diary entry %s\n", filepath); 252 | if (!g_file_delete (file, NULL, &err)) { 253 | g_warning ("Failed to remove file '%s': %s", filepath, err->message); 254 | g_object_unref (file); 255 | g_clear_error (&err); 256 | g_free (filepath); 257 | goto cleanup; 258 | } 259 | g_object_unref (file); 260 | 261 | basename = filename_to_basename (self->filename); 262 | 263 | photos_path = g_strdup_printf ("%s/photos/%s", self->folder, basename); 264 | g_print ("deleting photos in '%s'\n", photos_path); 265 | dir = g_dir_open(photos_path, 0, NULL); 266 | while (dir && (filename = g_dir_read_name(dir))) { 267 | gchar *filepath; 268 | GFile *file; 269 | 270 | filepath = g_strdup_printf ("%s/%s", photos_path, filename); 271 | file = g_file_new_for_path (filepath); 272 | if (!g_file_delete (file, NULL, &err)) { 273 | g_warning ("Failed to remove file '%s': %s", filepath, err->message); 274 | g_clear_error (&err); 275 | } 276 | 277 | g_object_unref (file); 278 | g_free (filepath); 279 | } 280 | if (g_remove (photos_path) < 0 && errno != ENOENT) { 281 | g_printerr ("Failed to delete photos folder: %s\n", strerror (errno)); 282 | } 283 | 284 | ret = TRUE; 285 | 286 | cleanup: 287 | g_signal_emit_by_name (self, "deleted"); 288 | g_free (photos_path); 289 | g_free (basename); 290 | g_free (filepath); 291 | if (dir) { 292 | g_dir_close (dir); 293 | } 294 | 295 | return ret; 296 | } 297 | 298 | static void 299 | entry_class_init (EntryClass *klass) 300 | { 301 | GObjectClass *object_class = G_OBJECT_CLASS (klass); 302 | 303 | object_class->get_property = entry_get_property; 304 | object_class->set_property = entry_set_property; 305 | 306 | obj_properties[PROP_FOLDER] = 307 | g_param_spec_string ("folder", 308 | "folder", 309 | "folder", 310 | NULL /* default value */, 311 | G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); 312 | obj_properties[PROP_FILENAME] = 313 | g_param_spec_string ("filename", 314 | "filename", 315 | "filename", 316 | NULL /* default value */, 317 | G_PARAM_READWRITE); 318 | obj_properties[PROP_BASENAME] = 319 | g_param_spec_string ("basename", 320 | "basename", 321 | "basename", 322 | NULL /* default value */, 323 | G_PARAM_READABLE); 324 | obj_properties[PROP_PATH] = 325 | g_param_spec_string ("filepath", 326 | "filepath", 327 | "Path of file where the diary entry is saved", 328 | NULL, 329 | G_PARAM_READABLE); 330 | 331 | g_object_class_install_properties (object_class, 332 | NUM_PROPS, 333 | obj_properties); 334 | 335 | obj_signals[SIGNAL_DELETED] = 336 | g_signal_newv ("deleted", 337 | G_TYPE_FROM_CLASS (object_class), 338 | 0, /* flags */ 339 | NULL /* closure */, 340 | NULL /* accumulator */, 341 | NULL /* accumulator data */, 342 | g_cclosure_marshal_VOID__VOID, 343 | G_TYPE_NONE /* return_type */, 344 | 0 /* n_params */, 345 | NULL /* param_types */); 346 | } 347 | 348 | static void 349 | entry_init (Entry *self) 350 | { 351 | self->folder = NULL; 352 | self->filename = NULL; 353 | } 354 | 355 | Entry * 356 | entry_new (const gchar *folder, const gchar *filename) 357 | { 358 | return g_object_new (DIARY_TYPE_ENTRY, 359 | "folder", folder, 360 | "filename", filename, 361 | NULL); 362 | } 363 | -------------------------------------------------------------------------------- /src/entry.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #define DIARY_TYPE_ENTRY (entry_get_type()) 6 | 7 | G_DECLARE_FINAL_TYPE (Entry, entry, DIARY, ENTRY, GObject) 8 | 9 | typedef struct _Entry Entry; 10 | 11 | Entry * entry_new (const gchar *folder, const gchar *filename); 12 | gchar * entry_read (Entry *self, GError **err); 13 | gboolean entry_rename_file (Entry *self, gchar *new_name); 14 | gboolean entry_write (Entry *self, gchar *text, GError **err); 15 | gboolean entry_delete (Entry *self); 16 | -------------------------------------------------------------------------------- /src/headerbuttons.c: -------------------------------------------------------------------------------- 1 | #include "headerbuttons.h" 2 | #include "window.h" 3 | 4 | G_DEFINE_INTERFACE (HeaderButtonsControl, diary_header_buttons_control, G_TYPE_OBJECT); 5 | 6 | void 7 | header_buttons_control_default_on_enter (GtkWidget *widget, GtkButton *new_button, GtkButton *back_button, GtkButton *settings_button) 8 | { 9 | g_object_set (new_button, "visible", FALSE, NULL); 10 | g_object_set (back_button, "visible", TRUE, NULL); 11 | g_object_set (settings_button, "visible", FALSE, NULL); 12 | } 13 | 14 | void 15 | header_buttons_control_default_on_leave (GtkWidget *widget, GtkButton *new_button, GtkButton *back_button, GtkButton *settings_button) 16 | { 17 | } 18 | 19 | void 20 | header_buttons_control_default_on_new_pressed (GtkWidget *widget) 21 | { 22 | } 23 | 24 | void 25 | header_buttons_control_default_on_back_pressed (GtkWidget *widget) 26 | { 27 | DiaryWindow *diary_window = diary_window_get_instance (); 28 | 29 | diary_window_pop_view (diary_window); 30 | } 31 | 32 | void 33 | diary_header_buttons_control_default_init (HeaderButtonsControlInterface *iface) 34 | { 35 | iface->on_enter = header_buttons_control_default_on_enter; 36 | iface->on_leave = header_buttons_control_default_on_leave; 37 | iface->on_new_pressed = header_buttons_control_default_on_new_pressed; 38 | iface->on_back_pressed = header_buttons_control_default_on_back_pressed; 39 | } 40 | -------------------------------------------------------------------------------- /src/headerbuttons.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #define DIARY_TYPE_HEADER_BUTTONS diary_header_buttons_control_get_type () 6 | G_DECLARE_INTERFACE (HeaderButtonsControl, diary_header_buttons_control, DIARY, HEADER_BUTTONS, GObject) 7 | 8 | struct _HeaderButtonsControlInterface 9 | { 10 | GTypeInterface parent_iface; 11 | 12 | /* called when entering the view */ 13 | void (*on_enter) (GtkWidget *widget, GtkButton *new_button, GtkButton *back_button, GtkButton *settings_button); 14 | /* called when leaving the view (either back or forward to some new page) */ 15 | void (*on_leave) (GtkWidget *widget, GtkButton *new_button, GtkButton *back_button, GtkButton *settings_button); 16 | /* called when pressing new */ 17 | void (*on_new_pressed) (GtkWidget *widget); 18 | /* called when pressing back */ 19 | void (*on_back_pressed) (GtkWidget *widget); 20 | }; 21 | 22 | void 23 | header_buttons_control_default_on_enter (GtkWidget *widget, GtkButton *new_button, GtkButton *back_button, GtkButton *settings_button); 24 | 25 | void 26 | header_buttons_control_default_on_leave (GtkWidget *widget, GtkButton *new_button, GtkButton *back_button, GtkButton *settings_button); 27 | 28 | void 29 | header_buttons_control_default_on_new_pressed (GtkWidget *widget); 30 | 31 | void 32 | header_buttons_control_default_on_back_pressed (GtkWidget *widget); 33 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "window.h" 6 | #include "utils.h" 7 | #include "settings.h" 8 | 9 | static void 10 | init(GtkApplication *app) 11 | { 12 | GtkApplicationWindow *window; 13 | gchar *diary_path; 14 | 15 | diary_path = utils_get_diary_folder (); 16 | g_print("Diary folder is '%s'\n", diary_path); 17 | if (g_mkdir_with_parents (diary_path, 0750) < 0) { 18 | gchar *msg = g_strdup_printf ( 19 | "Failed to create diary folder at '%s' because:\n%s\n", 20 | diary_path, strerror(errno)); 21 | g_printerr(msg); 22 | g_free (msg); 23 | return; 24 | } 25 | g_free (diary_path); 26 | 27 | utils_apply_color_scheme (); 28 | 29 | window = diary_window_new (app); 30 | gtk_widget_set_visible(GTK_WIDGET (window), TRUE); 31 | } 32 | 33 | int 34 | main(int argc, char *argv[]) 35 | { 36 | AdwApplication *app; 37 | 38 | gtk_init (); 39 | adw_init (); 40 | 41 | settings_init (); 42 | app = adw_application_new ("com.bjareholt.johan.SimpleDiary", G_APPLICATION_DEFAULT_FLAGS); 43 | g_signal_connect (app, "activate", G_CALLBACK (init), app); 44 | 45 | int status = g_application_run (G_APPLICATION (app), argc, argv); 46 | 47 | return status; 48 | } 49 | -------------------------------------------------------------------------------- /src/meson.build: -------------------------------------------------------------------------------- 1 | diary_src = [ 2 | gresources, 3 | 4 | 'entry.c', 5 | 'headerbuttons.c', 6 | 'main.c', 7 | 'notification.c', 8 | 'utils.c', 9 | 'settings.c', 10 | 'window.c', 11 | 12 | 'widgets/entry_browser.c', 13 | 'widgets/entry_view.c', 14 | 'widgets/entry_edit.c', 15 | 'widgets/entry_list.c', 16 | 'widgets/entry_listing.c', 17 | 'widgets/entry_name_dialog.c', 18 | 'widgets/entry_rename_dialog.c', 19 | 'widgets/entry_delete_dialog.c', 20 | 'widgets/image_picker.c', 21 | 'widgets/settings_view.c', 22 | ] 23 | 24 | diary_deps = [ 25 | gtk_dep, 26 | adwaita_dep, 27 | gdkpixbuf_dep, 28 | gtkmdview_dep, 29 | gtkmdeditor_dep, 30 | ] 31 | 32 | executable( 33 | 'simple-diary', diary_src, 34 | dependencies: diary_deps, 35 | install: true, 36 | gui_app: true, 37 | link_args: '-rdynamic', 38 | ) 39 | -------------------------------------------------------------------------------- /src/notification.c: -------------------------------------------------------------------------------- 1 | #include "notification.h" 2 | 3 | void notification_send (GApplication *application, gchar *title, gchar *body) { 4 | GNotification *notification; 5 | 6 | notification = g_notification_new (title); 7 | g_notification_set_body (notification, body); 8 | // TODO: Fix ID for each notification type 9 | g_application_send_notification (application, "unique-id", notification); 10 | g_object_unref (notification); 11 | } 12 | -------------------------------------------------------------------------------- /src/notification.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | void notification_send (GApplication *application, gchar *title, gchar *body); 6 | -------------------------------------------------------------------------------- /src/settings.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | #include "settings.h" 7 | 8 | static gchar *keyfile_path = NULL; 9 | static GKeyFile *keyfile = NULL; 10 | 11 | static gchar * 12 | config_dir(void) 13 | { 14 | return g_strdup_printf("%s/simple-diary", g_get_user_config_dir ()); 15 | } 16 | 17 | static gchar * 18 | default_data_dir(void) 19 | { 20 | return g_strdup_printf("%s/SimpleDiary", g_get_user_data_dir ()); 21 | } 22 | 23 | gchar * 24 | settings_get_diary_folder (void) 25 | { 26 | gchar *diary_folder; 27 | GError *err = NULL; 28 | 29 | g_assert_nonnull (keyfile); 30 | 31 | diary_folder = g_key_file_get_string (keyfile, "Notebooks", "Default", &err); 32 | if (err != NULL) { 33 | g_printerr ("Settings file is corrupted: %s\n", err->message); 34 | exit (EXIT_FAILURE); 35 | } 36 | 37 | return diary_folder; 38 | } 39 | 40 | ColorScheme 41 | settings_get_color_scheme (void) 42 | { 43 | ColorScheme color_scheme; 44 | g_autofree gchar *color_scheme_str = NULL; 45 | 46 | g_assert_nonnull (keyfile); 47 | 48 | color_scheme_str = g_key_file_get_string (keyfile, "Appearance", "ColorScheme", NULL); 49 | if (color_scheme_str == NULL) { 50 | g_print ("Could not find Appearance.ColorScheme, assuming default\n"); 51 | g_strdup ("default"); 52 | } 53 | 54 | if (g_strcmp0 (color_scheme_str, "light") == 0) { 55 | color_scheme = COLOR_SCHEME_LIGHT; 56 | } else if (g_strcmp0 (color_scheme_str, "default") == 0) { 57 | color_scheme = COLOR_SCHEME_DEFAULT; 58 | } else if (g_strcmp0 (color_scheme_str, "dark") == 0) { 59 | color_scheme = COLOR_SCHEME_DARK; 60 | } else { 61 | g_print ("Invalid color scheme '%s' in settings, assuming default\n", 62 | color_scheme_str); 63 | color_scheme = COLOR_SCHEME_DEFAULT; 64 | } 65 | 66 | return color_scheme; 67 | } 68 | 69 | void 70 | settings_set_color_scheme (ColorScheme color_scheme) 71 | { 72 | GError *err = NULL; 73 | gchar *color_scheme_str = NULL; 74 | 75 | g_assert_nonnull (keyfile); 76 | 77 | switch (color_scheme) { 78 | case COLOR_SCHEME_LIGHT: 79 | color_scheme_str = "light"; 80 | break; 81 | case COLOR_SCHEME_DEFAULT: 82 | color_scheme_str = "default"; 83 | break; 84 | case COLOR_SCHEME_DARK: 85 | color_scheme_str = "dark"; 86 | break; 87 | } 88 | 89 | g_print ("Saving color scheme preference: %s\n", color_scheme_str); 90 | 91 | g_key_file_set_string (keyfile, "Appearance", "ColorScheme", 92 | color_scheme_str); 93 | 94 | if (!g_key_file_save_to_file (keyfile, keyfile_path, &err)) { 95 | g_warning ("Error saving settings file: %s", err->message); 96 | exit (EXIT_FAILURE); 97 | } 98 | } 99 | 100 | /* If some required setting is missing, this function will return TRUE */ 101 | static gboolean 102 | settings_load_default_config (void) 103 | { 104 | gboolean write = FALSE; 105 | GError *err = NULL; 106 | g_autofree gchar *notebook = NULL; 107 | 108 | // Notebooks.Default 109 | notebook = g_key_file_get_string (keyfile, "Notebooks", "Default", &err); 110 | if (err != NULL) { 111 | if (g_error_matches (err, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND) || 112 | g_error_matches (err, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND)) { 113 | g_autofree gchar *diary_folder = default_data_dir(); 114 | g_key_file_set_string (keyfile, "Notebooks", "Default", diary_folder); 115 | write = TRUE; 116 | } else { 117 | g_printerr ("Settings file is corrupted: %s\n", err->message); 118 | exit (EXIT_FAILURE); 119 | } 120 | g_clear_error (&err); 121 | } 122 | 123 | /* Appearance.DarkMode */ 124 | g_key_file_get_boolean (keyfile, "Appearance", "DarkMode", &err); 125 | if (err != NULL) { 126 | if (g_error_matches (err, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND) || 127 | g_error_matches (err, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND)) { 128 | g_key_file_set_boolean (keyfile, "Appearance", "DarkMode", FALSE); 129 | write = TRUE; 130 | } else { 131 | g_printerr ("Settings file is corrupted: %s\n", err->message); 132 | exit (EXIT_FAILURE); 133 | } 134 | g_clear_error (&err); 135 | } 136 | 137 | return write; 138 | } 139 | 140 | void 141 | settings_init (void) 142 | { 143 | g_autofree gchar *config_folder = NULL; 144 | GError *err = NULL; 145 | 146 | g_assert_null (keyfile); 147 | 148 | config_folder = config_dir(); 149 | g_print("Config folder is '%s'\n", config_folder); 150 | if (g_mkdir_with_parents (config_folder, 0750) < 0) { 151 | g_printerr ("Failed to create config folder: %s\n", err->message); 152 | exit (EXIT_FAILURE); 153 | } 154 | 155 | keyfile = g_key_file_new (); 156 | keyfile_path = g_strdup_printf ("%s/settings.ini", config_folder); 157 | 158 | if (!g_key_file_load_from_file (keyfile, keyfile_path, 0, &err)) { 159 | if (g_error_matches (err, G_FILE_ERROR, G_FILE_ERROR_NOENT)) { 160 | // TODO: create default 161 | g_print ("Writing new config\n"); 162 | g_clear_error (&err); 163 | } else { 164 | g_printerr ("Failed to load config: %s\n", err->message); 165 | exit (EXIT_FAILURE); 166 | } 167 | } 168 | 169 | if (settings_load_default_config ()) { 170 | /* settings file has changed, needs to be written */ 171 | g_print ("Writing config at '%s'\n", keyfile_path); 172 | if (!g_key_file_save_to_file (keyfile, keyfile_path, &err)) { 173 | g_warning ("Error saving settings file: %s", err->message); 174 | exit (EXIT_FAILURE); 175 | } 176 | } 177 | } 178 | -------------------------------------------------------------------------------- /src/settings.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | enum _ColorScheme { 4 | COLOR_SCHEME_LIGHT, 5 | COLOR_SCHEME_DEFAULT, 6 | COLOR_SCHEME_DARK, 7 | }; 8 | 9 | typedef enum _ColorScheme ColorScheme; 10 | 11 | void settings_init (void); 12 | 13 | gchar * settings_get_diary_folder (void); 14 | 15 | ColorScheme settings_get_color_scheme (void); 16 | void settings_set_color_scheme (ColorScheme color_scheme); 17 | -------------------------------------------------------------------------------- /src/utils.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "utils.h" 7 | #include "window.h" 8 | #include "settings.h" 9 | 10 | static void 11 | error_dialog_cb (AdwMessageDialog *dialog, 12 | GAsyncResult *result, 13 | gpointer user_data) 14 | { 15 | } 16 | 17 | void 18 | utils_error_dialog(GtkWidget *parent, gchar *format, ...) 19 | { 20 | AdwDialog *dialog; 21 | gchar *message; 22 | va_list args; 23 | 24 | va_start (args, format); 25 | g_vasprintf (&message, format, args); 26 | g_printerr("Error: %s", message); 27 | 28 | dialog = adw_alert_dialog_new ("Error", message); 29 | 30 | 31 | adw_alert_dialog_add_responses (ADW_ALERT_DIALOG (dialog), 32 | "close", "_Close", 33 | NULL); 34 | adw_alert_dialog_set_response_appearance (ADW_ALERT_DIALOG (dialog), "close", ADW_RESPONSE_DESTRUCTIVE); 35 | 36 | adw_alert_dialog_choose (ADW_ALERT_DIALOG (dialog), parent, NULL, (GAsyncReadyCallback) error_dialog_cb, NULL); 37 | } 38 | 39 | gchar * 40 | utils_get_diary_folder (void) 41 | { 42 | return settings_get_diary_folder (); 43 | } 44 | 45 | /*TODO: Move this to entry class? */ 46 | gchar * 47 | utils_get_photos_folder (gchar *basename, gboolean absolute) 48 | { 49 | gchar *diary_folder; 50 | gchar *photos_folder_absolute; 51 | 52 | diary_folder = utils_get_diary_folder (); 53 | photos_folder_absolute = g_strdup_printf ("%s/photos/%s", 54 | diary_folder, basename); 55 | g_free (diary_folder); 56 | 57 | if (g_mkdir_with_parents (photos_folder_absolute, 0750) < 0) { 58 | gchar *msg; 59 | 60 | msg = g_strdup_printf ("Failed to create folder at '%s' because: %s\n", 61 | photos_folder_absolute, strerror(errno)); 62 | GtkWindow *window; 63 | window = GTK_WINDOW (diary_window_get_instance ()); 64 | utils_error_dialog(GTK_WIDGET (window), msg); 65 | g_free (msg); 66 | g_free (photos_folder_absolute); 67 | photos_folder_absolute = NULL; 68 | } 69 | 70 | if (absolute) { 71 | return photos_folder_absolute; 72 | } else { 73 | g_free (photos_folder_absolute); 74 | return g_strdup_printf ("photos/%s", basename); 75 | } 76 | } 77 | 78 | gchar * 79 | utils_get_file_extension (gchar *filepath) 80 | { 81 | return strrchr (filepath, '.'); 82 | } 83 | 84 | void 85 | utils_apply_color_scheme (void) 86 | { 87 | AdwApplication *app; 88 | AdwStyleManager *style_mgr; 89 | ColorScheme color_scheme; 90 | 91 | color_scheme = settings_get_color_scheme (); 92 | 93 | app = ADW_APPLICATION (g_application_get_default ()); 94 | style_mgr = adw_application_get_style_manager (app); 95 | 96 | switch (color_scheme) { 97 | case COLOR_SCHEME_LIGHT: 98 | g_object_set (style_mgr, "color-scheme", ADW_COLOR_SCHEME_PREFER_LIGHT, NULL); 99 | break; 100 | case COLOR_SCHEME_DEFAULT: 101 | g_object_set (style_mgr, "color-scheme", ADW_COLOR_SCHEME_DEFAULT, NULL); 102 | break; 103 | case COLOR_SCHEME_DARK: 104 | g_object_set (style_mgr, "color-scheme", ADW_COLOR_SCHEME_PREFER_DARK, NULL); 105 | break; 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/utils.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | void utils_error_dialog (GtkWidget *widget, gchar *message, ...); 7 | 8 | gchar *utils_get_diary_folder (void); 9 | 10 | gchar *utils_get_photos_folder (gchar *basename, gboolean absolute); 11 | 12 | gchar *utils_get_file_extension (gchar *filepath); 13 | 14 | void utils_apply_color_scheme (void); 15 | -------------------------------------------------------------------------------- /src/widgets/entry_browser.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "headerbuttons.h" 4 | #include "entry_browser.h" 5 | #include "entry_list.h" 6 | #include "entry_listing.h" 7 | #include "entry_edit.h" 8 | #include "entry_view.h" 9 | #include "entry_name_dialog.h" 10 | #include "entry.h" 11 | #include "window.h" 12 | #include "utils.h" 13 | 14 | struct _EntryBrowser 15 | { 16 | AdwBreakpointBin parent_instance; 17 | 18 | AdwNavigationSplitView *split_view; 19 | EntryList *entry_list; 20 | GtkBox *entry_list_box; 21 | GtkBox *content_box; 22 | GtkWidget *content_box_child; 23 | 24 | /* from header_buttons_control */ 25 | GtkButton *back_button; 26 | GtkButton *new_button; 27 | }; 28 | 29 | static void 30 | update_header_buttons (EntryBrowser *self) 31 | { 32 | if (self->content_box_child == NULL) { 33 | g_object_set (self->back_button, "visible", FALSE, NULL); 34 | g_object_set (self->new_button, "visible", TRUE, NULL); 35 | } else { 36 | g_object_set (self->back_button, "visible", TRUE, NULL); 37 | g_object_set (self->new_button, "visible", FALSE, NULL); 38 | } 39 | } 40 | 41 | static void 42 | entry_browser_set_content (EntryBrowser *self, GtkWidget *widget) 43 | { 44 | // Remove old content 45 | if (self->content_box_child != NULL) { 46 | gtk_box_remove (self->content_box, self->content_box_child); 47 | } 48 | 49 | // Add new content if there is any 50 | if (widget == NULL) { 51 | self->content_box_child = NULL; 52 | g_object_set(self->split_view, "show-content", FALSE, NULL); 53 | } else { 54 | self->content_box_child = widget; 55 | gtk_box_append (self->content_box, self->content_box_child); 56 | g_object_set(self->split_view, "show-content", TRUE, NULL); 57 | } 58 | 59 | update_header_buttons (self); 60 | } 61 | 62 | static void 63 | on_enter (GtkWidget *widget, GtkButton *new_button, GtkButton *back_button, GtkButton *settings_button) 64 | { 65 | EntryBrowser *self = DIARY_ENTRY_BROWSER (widget); 66 | 67 | self->new_button = new_button; 68 | self->back_button = back_button; 69 | 70 | g_object_set (settings_button, "visible", TRUE, NULL); 71 | update_header_buttons (self); 72 | } 73 | 74 | static void 75 | on_leave (GtkWidget *widget, GtkButton *new_button, GtkButton *back_button, GtkButton *settings_button) 76 | { 77 | EntryBrowser *self = DIARY_ENTRY_BROWSER (widget); 78 | g_print ("leave %p\n", self); 79 | } 80 | 81 | static void 82 | entry_view_deleted_cb (EntryView *entry_view, Entry *entry, gpointer user_data) 83 | { 84 | gboolean removed; 85 | EntryBrowser *browser = DIARY_ENTRY_BROWSER (user_data); 86 | entry_browser_set_content (browser, NULL); 87 | removed = entry_list_remove (browser->entry_list, entry); 88 | g_assert (removed); 89 | } 90 | 91 | void 92 | entry_name_cb (GObject *obj, 93 | GAsyncResult *res, 94 | gpointer user_data) 95 | { 96 | EntryNameDialog *dialog = DIARY_ENTRY_NAME_DIALOG (obj); 97 | EntryBrowser *self = DIARY_ENTRY_BROWSER (user_data); 98 | 99 | g_autofree gchar *name = entry_name_dialog_open_finish(dialog, res); 100 | if (name != NULL) { 101 | gchar *text; 102 | gchar *filename; 103 | DiaryWindow *diary_window; 104 | GtkListBoxRow *entry_listing_row; 105 | EntryListing *entry_listing; 106 | GtkWidget *entry_view; 107 | GtkWidget *entry_edit; 108 | Entry *entry; 109 | 110 | diary_window = diary_window_get_instance (); 111 | 112 | text = entry_name_dialog_get_name (dialog); 113 | // TODO: check if name contains dots or slashes 114 | filename = g_strdup_printf ("%s.md", text); 115 | 116 | entry_listing_row = entry_list_find (self->entry_list, filename); 117 | if (entry_listing_row != NULL) { 118 | g_free (filename); 119 | entry_listing = DIARY_ENTRY_LISTING (gtk_list_box_row_get_child (entry_listing_row)); 120 | entry = entry_listing_get_entry (entry_listing); 121 | } else { 122 | gchar *folder = utils_get_diary_folder (); 123 | entry = entry_new (folder, filename); 124 | g_free (folder); 125 | entry_list_add_entry (self->entry_list, entry); 126 | entry_listing_row = entry_list_find (self->entry_list, filename); 127 | } 128 | 129 | entry_edit = entry_edit_new (entry); 130 | entry_view = entry_view_new (entry); 131 | g_signal_connect (entry_view, "deleted", G_CALLBACK (entry_view_deleted_cb), self); 132 | entry_list_focus (self->entry_list, entry_listing_row); 133 | diary_window_push_view (diary_window, entry_edit); 134 | 135 | g_free (text); 136 | g_free (filename); 137 | } 138 | } 139 | 140 | static void 141 | on_new_pressed (GtkWidget *widget) 142 | { 143 | EntryBrowser *self = DIARY_ENTRY_BROWSER (widget); 144 | GtkWindow *window; 145 | GtkWidget *dialog; 146 | 147 | window = GTK_WINDOW (diary_window_get_instance ()); 148 | 149 | dialog = entry_name_dialog_new (); 150 | 151 | entry_name_dialog_open(DIARY_ENTRY_NAME_DIALOG (dialog), window, entry_name_cb, self); 152 | } 153 | 154 | static void 155 | on_back_pressed (GtkWidget *widget) 156 | { 157 | EntryBrowser *self = DIARY_ENTRY_BROWSER (widget); 158 | entry_browser_set_content (self, NULL); 159 | entry_list_unfocus (self->entry_list); 160 | } 161 | 162 | static void 163 | diary_header_buttons_control_init (HeaderButtonsControlInterface *iface) 164 | { 165 | iface->on_enter = on_enter; 166 | iface->on_leave = on_leave; 167 | iface->on_new_pressed = on_new_pressed; 168 | iface->on_back_pressed = on_back_pressed; 169 | } 170 | 171 | G_DEFINE_TYPE_WITH_CODE (EntryBrowser, entry_browser, ADW_TYPE_BREAKPOINT_BIN, 172 | G_IMPLEMENT_INTERFACE (DIARY_TYPE_HEADER_BUTTONS, diary_header_buttons_control_init)); 173 | 174 | static void 175 | entry_selected_changed_cb (EntryList *list, Entry *entry, EntryBrowser *browser) 176 | { 177 | if (entry == NULL) { 178 | entry_browser_set_content (browser, NULL); 179 | } else { 180 | GtkWidget *entry_view = entry_view_new (entry); 181 | g_signal_connect (entry_view, "deleted", G_CALLBACK (entry_view_deleted_cb), browser); 182 | entry_browser_set_content (browser, entry_view); 183 | } 184 | } 185 | 186 | static void 187 | entry_browser_class_init (EntryBrowserClass *klass) 188 | { 189 | GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); 190 | 191 | gtk_widget_class_set_template_from_resource (widget_class, "/com/bjareholt/johan/simple-diary/ui/entry_browser.ui"); 192 | gtk_widget_class_bind_template_child (widget_class, EntryBrowser, split_view); 193 | gtk_widget_class_bind_template_child (widget_class, EntryBrowser, entry_list_box); 194 | gtk_widget_class_bind_template_child (widget_class, EntryBrowser, content_box); 195 | } 196 | 197 | static void 198 | entry_browser_init (EntryBrowser *self) 199 | { 200 | self->content_box_child = NULL; 201 | 202 | gtk_widget_init_template (GTK_WIDGET (self)); 203 | 204 | self->entry_list = DIARY_ENTRY_LIST (entry_list_new ()); 205 | gtk_box_append (self->entry_list_box, GTK_WIDGET (self->entry_list)); 206 | 207 | g_signal_connect (self->entry_list, "selection-changed", G_CALLBACK (entry_selected_changed_cb), self); 208 | } 209 | 210 | GtkWidget * 211 | entry_browser_new (void) 212 | { 213 | EntryBrowser *browser = g_object_new (DIARY_TYPE_ENTRY_BROWSER, NULL); 214 | 215 | return GTK_WIDGET (browser); 216 | } 217 | -------------------------------------------------------------------------------- /src/widgets/entry_browser.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #define DIARY_TYPE_ENTRY_BROWSER (entry_browser_get_type()) 7 | 8 | G_DECLARE_FINAL_TYPE (EntryBrowser, entry_browser, DIARY, ENTRY_BROWSER, AdwBreakpointBin) 9 | 10 | GtkWidget * 11 | entry_browser_new (void); 12 | -------------------------------------------------------------------------------- /src/widgets/entry_delete_dialog.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "widgets/entry_delete_dialog.h" 4 | 5 | struct _EntryDeleteDialog 6 | { 7 | GtkWindow parent_instance; 8 | 9 | /* widgets */ 10 | GtkButton *button_cancel; 11 | GtkButton *button_ok; 12 | }; 13 | 14 | struct _EntryDeleteDialogClass 15 | { 16 | GtkWindowClass parent_class; 17 | }; 18 | 19 | G_DEFINE_TYPE (EntryDeleteDialog, entry_delete_dialog, GTK_TYPE_WINDOW); 20 | 21 | static void 22 | entry_delete_dialog_class_init (EntryDeleteDialogClass *klass) 23 | { 24 | GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); 25 | 26 | gtk_widget_class_set_template_from_resource (widget_class, 27 | "/com/bjareholt/johan/simple-diary/ui/entry_delete_dialog.ui"); 28 | gtk_widget_class_bind_template_child (widget_class, EntryDeleteDialog, 29 | button_cancel); 30 | gtk_widget_class_bind_template_child (widget_class, EntryDeleteDialog, 31 | button_ok); 32 | } 33 | 34 | static void 35 | entry_delete_dialog_init (EntryDeleteDialog *self) 36 | { 37 | gtk_widget_init_template (GTK_WIDGET (self)); 38 | } 39 | 40 | GtkWidget * 41 | entry_delete_dialog_new () 42 | { 43 | EntryDeleteDialog *entry_delete_dialog; 44 | 45 | entry_delete_dialog = g_object_new (DIARY_TYPE_ENTRY_DELETE_DIALOG, NULL); 46 | 47 | return GTK_WIDGET (entry_delete_dialog); 48 | } 49 | 50 | static void 51 | dialog_response (GtkEntry *entry, 52 | gpointer user_data) 53 | { 54 | GTask *task = G_TASK (user_data); 55 | g_task_return_boolean (task, TRUE); 56 | } 57 | 58 | static void 59 | dialog_cancel (GtkEntry *entry, 60 | gpointer user_data) 61 | { 62 | GTask *task = G_TASK (user_data); 63 | g_task_return_boolean (task, FALSE); 64 | } 65 | 66 | void 67 | entry_delete_dialog_open (EntryDeleteDialog *self, 68 | GtkWindow *parent, 69 | GAsyncReadyCallback callback, 70 | gpointer user_data) 71 | { 72 | GTask *task; 73 | 74 | task = g_task_new (self, FALSE, callback, user_data); 75 | g_task_set_check_cancellable (task, FALSE); 76 | g_task_set_source_tag (task, entry_delete_dialog_open); 77 | g_task_set_task_data (task, self, g_object_unref); 78 | 79 | g_signal_connect (self->button_ok, "clicked", G_CALLBACK (dialog_response), task); 80 | g_signal_connect (self->button_cancel, "clicked", G_CALLBACK (dialog_cancel), task); 81 | 82 | gtk_window_set_transient_for (GTK_WINDOW (self), parent); 83 | gtk_widget_set_visible (GTK_WIDGET (self), TRUE); 84 | } 85 | 86 | gboolean 87 | entry_delete_dialog_open_finish (EntryDeleteDialog *self, 88 | GAsyncResult *result) 89 | { 90 | gtk_window_destroy (GTK_WINDOW (self)); 91 | 92 | return g_task_propagate_boolean (G_TASK (result), NULL); 93 | } 94 | -------------------------------------------------------------------------------- /src/widgets/entry_delete_dialog.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "entry.h" 6 | 7 | G_BEGIN_DECLS 8 | 9 | #define DIARY_TYPE_ENTRY_DELETE_DIALOG (entry_delete_dialog_get_type()) 10 | 11 | #define DIARY_TYPE_IS_ENTRY_DELETE_DIALOG (object) \ 12 | (G_OBJECT_TYPE (object) == DIARY_TYPE_ENTRY_DELETE_DIALOG) 13 | 14 | G_DECLARE_FINAL_TYPE (EntryDeleteDialog, entry_delete_dialog, DIARY, ENTRY_DELETE_DIALOG, GtkWindow) 15 | 16 | GtkWidget * 17 | entry_delete_dialog_new (); 18 | 19 | void 20 | entry_delete_dialog_open (EntryDeleteDialog *self, 21 | GtkWindow *parent, 22 | GAsyncReadyCallback callback, 23 | gpointer user_data); 24 | 25 | gboolean 26 | entry_delete_dialog_open_finish (EntryDeleteDialog *self, 27 | GAsyncResult *result); 28 | 29 | G_END_DECLS 30 | -------------------------------------------------------------------------------- /src/widgets/entry_edit.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "entry_edit.h" 4 | #include "image_picker.h" 5 | #include "utils.h" 6 | #include "window.h" 7 | #include "headerbuttons.h" 8 | 9 | struct _EntryEdit 10 | { 11 | GtkBox parent_instance; 12 | 13 | /* widgets */ 14 | GtkScrolledWindow *entry_edit_viewport; 15 | GtkMdEditor *md_editor; 16 | GtkButton *add_image_button; 17 | 18 | /* properties */ 19 | Entry *entry; 20 | gboolean unsaved_changes; 21 | }; 22 | 23 | static void entry_edit_save (EntryEdit *self); 24 | 25 | static void 26 | on_back_pressed (GtkWidget *widget) 27 | { 28 | EntryEdit *self = DIARY_ENTRY_EDIT (widget); 29 | entry_edit_save (self); 30 | header_buttons_control_default_on_back_pressed (widget); 31 | } 32 | 33 | static void 34 | diary_header_buttons_control_init (HeaderButtonsControlInterface *iface) 35 | { 36 | iface->on_back_pressed = on_back_pressed; 37 | } 38 | 39 | G_DEFINE_TYPE_WITH_CODE (EntryEdit, entry_edit, GTK_TYPE_BOX, 40 | G_IMPLEMENT_INTERFACE (DIARY_TYPE_HEADER_BUTTONS, diary_header_buttons_control_init)); 41 | 42 | typedef enum 43 | { 44 | PROP_ENTRY = 1, 45 | NUM_PROPS, 46 | } EntryProperties; 47 | 48 | static GParamSpec *obj_properties[NUM_PROPS] = { NULL, }; 49 | 50 | static void 51 | entry_edit_set_property (GObject *object, 52 | guint property_id, 53 | const GValue *value, 54 | GParamSpec *pspec) 55 | { 56 | EntryEdit *self = DIARY_ENTRY_EDIT (object); 57 | 58 | switch ((EntryProperties) property_id) { 59 | case PROP_ENTRY: 60 | self->entry = g_value_get_object(value); 61 | break; 62 | default: 63 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); 64 | break; 65 | } 66 | } 67 | 68 | static void 69 | entry_edit_get_property (GObject *object, 70 | guint property_id, 71 | GValue *value, 72 | GParamSpec *pspec) 73 | { 74 | EntryEdit *self = DIARY_ENTRY_EDIT (object); 75 | 76 | switch ((EntryProperties) property_id) 77 | { 78 | case PROP_ENTRY: 79 | g_value_set_object (value, self->entry); 80 | break; 81 | default: 82 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); 83 | break; 84 | } 85 | } 86 | 87 | static void entry_edit_read(EntryEdit *self) 88 | { 89 | GError *err = NULL; 90 | GtkTextBuffer *buffer; 91 | gchar *body; 92 | 93 | body = entry_read (self->entry, &err); 94 | if (err != NULL) { 95 | g_warning ("Failed to read entry"); 96 | goto cleanup; 97 | } 98 | 99 | buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW(self->md_editor)); 100 | gtk_text_buffer_set_text (buffer, body, -1); 101 | 102 | cleanup: 103 | if (body != NULL) { 104 | g_free (body); 105 | } 106 | } 107 | 108 | static void 109 | entry_edit_save(EntryEdit *self) 110 | { 111 | GtkTextIter start, end; 112 | gchar *text; 113 | GtkTextBuffer *buffer; 114 | GError *err = NULL; 115 | 116 | if (self->unsaved_changes == FALSE) 117 | return; 118 | 119 | buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW(self->md_editor)); 120 | gtk_text_buffer_get_bounds (buffer, &start, &end); 121 | text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE); 122 | 123 | if (!entry_write(self->entry, text, &err)) { 124 | g_error ("failed to save entry: %s\n", err->message); 125 | g_clear_error (&err); 126 | goto cleanup; 127 | } 128 | 129 | self->unsaved_changes = FALSE; 130 | 131 | cleanup: 132 | g_free (text); 133 | } 134 | 135 | static void 136 | entry_edit_constructed (GObject *object) 137 | { 138 | EntryEdit *self = DIARY_ENTRY_EDIT (object); 139 | entry_edit_read (self); 140 | 141 | G_OBJECT_CLASS (entry_edit_parent_class)->constructed (object); 142 | } 143 | 144 | void 145 | image_picker_cb (GObject *obj, 146 | GAsyncResult *res, 147 | gpointer user_data) 148 | { 149 | ImagePicker *image_picker = DIARY_IMAGE_PICKER_DIALOG (obj); 150 | EntryEdit *entry_edit = DIARY_ENTRY_EDIT (user_data); 151 | 152 | g_autoptr(Image) image = image_picker_dialog_open_finish(image_picker, res); 153 | if (image != NULL) { 154 | g_print ("Adding image named '%s' from path '%s'\n", image->name, image->path); 155 | GtkTextBuffer *text_buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW(entry_edit->md_editor)); 156 | g_autofree char *md_image_link = g_strdup_printf ("![%s](<%s>)", image->name, image->path); 157 | gtk_text_buffer_insert_at_cursor (text_buffer, md_image_link, strlen (md_image_link)); 158 | } 159 | } 160 | 161 | static gboolean 162 | add_image_button_clicked(GtkButton *button, gpointer user_data) 163 | { 164 | EntryEdit *entry_edit = DIARY_ENTRY_EDIT (user_data); 165 | GtkWindow *window; 166 | g_autofree gchar *basename; 167 | 168 | window = GTK_WINDOW (diary_window_get_instance ()); 169 | g_object_get (entry_edit->entry, "basename", &basename, NULL); 170 | 171 | ImagePicker *image_picker = image_picker_dialog_new (basename); 172 | image_picker_dialog_open (image_picker, window, image_picker_cb, entry_edit); 173 | 174 | return TRUE; 175 | } 176 | 177 | static void 178 | entry_edit_class_init (EntryEditClass *klass) 179 | { 180 | GObjectClass *object_class = G_OBJECT_CLASS (klass); 181 | GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); 182 | 183 | object_class->constructed = entry_edit_constructed; 184 | 185 | object_class->get_property = entry_edit_get_property; 186 | object_class->set_property = entry_edit_set_property; 187 | 188 | obj_properties[PROP_ENTRY] = 189 | g_param_spec_object ("entry", 190 | "entry", 191 | "entry", 192 | DIARY_TYPE_ENTRY, 193 | G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); 194 | 195 | g_object_class_install_properties (object_class, 196 | NUM_PROPS, 197 | obj_properties); 198 | 199 | gtk_widget_class_set_template_from_resource (widget_class, 200 | "/com/bjareholt/johan/simple-diary/ui/entry_edit.ui"); 201 | gtk_widget_class_bind_template_child (widget_class, EntryEdit, entry_edit_viewport); 202 | gtk_widget_class_bind_template_child (widget_class, EntryEdit, add_image_button); 203 | } 204 | 205 | static void 206 | entry_edit_init (EntryEdit *self) 207 | { 208 | gtk_widget_init_template (GTK_WIDGET (self)); 209 | self->unsaved_changes = TRUE; 210 | g_signal_connect (self, "destroy", (GCallback) entry_edit_save, NULL); 211 | g_signal_connect (self->add_image_button, "clicked", 212 | G_CALLBACK (add_image_button_clicked), self); 213 | self->md_editor = GTK_MD_EDITOR(gtk_md_editor_new()); 214 | g_object_set(self->entry_edit_viewport, "child", self->md_editor, NULL); 215 | } 216 | 217 | GtkWidget * 218 | entry_edit_new (Entry *entry) 219 | { 220 | EntryEdit *entry_edit = g_object_new (DIARY_TYPE_ENTRY_EDIT, "entry", entry, NULL); 221 | 222 | return GTK_WIDGET (entry_edit); 223 | } 224 | -------------------------------------------------------------------------------- /src/widgets/entry_edit.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "entry.h" 6 | 7 | #define DIARY_TYPE_ENTRY_EDIT (entry_edit_get_type()) 8 | 9 | G_DECLARE_FINAL_TYPE (EntryEdit, entry_edit, DIARY, ENTRY_EDIT, GtkBox) 10 | 11 | GtkWidget * 12 | entry_edit_new (Entry *entry); 13 | -------------------------------------------------------------------------------- /src/widgets/entry_list.c: -------------------------------------------------------------------------------- 1 | #include "utils.h" 2 | #include "entry.h" 3 | #include "entry_listing.h" 4 | #include "entry_list.h" 5 | 6 | struct _EntryList 7 | { 8 | GtkWindow parent_instance; 9 | 10 | GtkListBox *entry_list_box; 11 | }; 12 | 13 | enum 14 | { 15 | SIGNAL_SELECTION_CHANGED, 16 | LAST_SIGNAL, 17 | }; 18 | 19 | static guint entry_list_signals[LAST_SIGNAL] = { 0 }; 20 | 21 | G_DEFINE_TYPE (EntryList, entry_list, GTK_TYPE_SCROLLED_WINDOW); 22 | 23 | static gint 24 | list_box_sort (GtkListBoxRow *row1, GtkListBoxRow *row2, gpointer user_data) 25 | { 26 | EntryListing *listing1; 27 | EntryListing *listing2; 28 | Entry *entry1; 29 | Entry *entry2; 30 | gchar *name1; 31 | gchar *name2; 32 | gint ret; 33 | 34 | listing1 = DIARY_ENTRY_LISTING (gtk_list_box_row_get_child (GTK_LIST_BOX_ROW (row1))); 35 | listing2 = DIARY_ENTRY_LISTING (gtk_list_box_row_get_child (GTK_LIST_BOX_ROW (row2))); 36 | 37 | entry1 = entry_listing_get_entry (listing1); 38 | entry2 = entry_listing_get_entry (listing2); 39 | 40 | g_object_get (entry1, "filename", &name1, NULL); 41 | g_object_get (entry2, "filename", &name2, NULL); 42 | 43 | ret = -g_strcmp0 (name1, name2); 44 | 45 | g_free (name1); 46 | g_free (name2); 47 | 48 | return ret; 49 | } 50 | 51 | static GPtrArray * 52 | list_entries (const gchar * dir_path) 53 | { 54 | GDir *dir; 55 | GError *error; 56 | const gchar *filename; 57 | GPtrArray *files; 58 | files = g_ptr_array_new_full(50, g_free); 59 | 60 | dir = g_dir_open(dir_path, 0, &error); 61 | while ((filename = g_dir_read_name(dir))) { 62 | gchar *file_path = g_strdup_printf ("%s/%s", dir_path, filename); 63 | if (g_file_test (file_path, G_FILE_TEST_IS_REGULAR) && 64 | g_str_has_suffix (file_path, ".md")) { 65 | g_ptr_array_add (files, g_strdup (filename)); 66 | } 67 | g_free (file_path); 68 | } 69 | 70 | g_dir_close (dir); 71 | 72 | return files; 73 | } 74 | 75 | static void 76 | entry_pressed_cb (GtkListBox *entry_list_box, GtkListBoxRow *row, gpointer user_data) 77 | { 78 | /* necessary for making no entry selected by default */ 79 | gtk_list_box_set_selection_mode (entry_list_box, GTK_SELECTION_SINGLE); 80 | gtk_list_box_select_row (entry_list_box, row); 81 | } 82 | 83 | static void 84 | entry_selected_changed_cb (GtkListBox *entry_list_box, gpointer user_data) 85 | { 86 | EntryList *list = DIARY_ENTRY_LIST (user_data); 87 | GtkListBoxRow *row; 88 | Entry *entry = NULL; 89 | 90 | row = gtk_list_box_get_selected_row (GTK_LIST_BOX (entry_list_box)); 91 | if (row) { 92 | gchar *name; 93 | GtkWidget *child; 94 | EntryListing *listing; 95 | 96 | child = gtk_list_box_row_get_child (row); 97 | listing = DIARY_ENTRY_LISTING (child); 98 | entry = entry_listing_get_entry (listing); 99 | g_object_get (entry, "basename", &name, NULL); 100 | g_print ("%s\n", name); 101 | g_free (name); 102 | } else { 103 | g_print ("entry unselected\n"); 104 | } 105 | 106 | g_signal_emit_by_name (list, "selection-changed", entry); 107 | } 108 | 109 | void 110 | entry_list_add_entry (EntryList *self, Entry *entry) 111 | { 112 | GtkWidget *entry_listing = GTK_WIDGET (entry_listing_new (entry)); 113 | GtkWidget *row_widget = gtk_list_box_row_new (); 114 | gtk_list_box_row_set_child (GTK_LIST_BOX_ROW (row_widget), entry_listing); 115 | gtk_list_box_insert (self->entry_list_box, row_widget, -1); 116 | } 117 | 118 | static GtkListBox * 119 | generate_entry_list (EntryList *self, const gchar *dir_path, GPtrArray *files) 120 | { 121 | const gchar *filename; 122 | 123 | gtk_list_box_set_selection_mode (self->entry_list_box, GTK_SELECTION_NONE); 124 | 125 | g_signal_connect (self->entry_list_box, "row-activated", G_CALLBACK (entry_pressed_cb), NULL); 126 | g_signal_connect (self->entry_list_box, "selected-rows-changed", G_CALLBACK (entry_selected_changed_cb), self); 127 | 128 | for (int i=0; i < files->len; i++) { 129 | filename = g_ptr_array_index (files, i); 130 | Entry *entry = entry_new (dir_path, filename); 131 | entry_list_add_entry (self, entry); 132 | } 133 | 134 | return self->entry_list_box; 135 | } 136 | 137 | static void 138 | load_entry_list (EntryList *self) 139 | { 140 | gchar *dir_path; 141 | GPtrArray *files; 142 | 143 | dir_path = utils_get_diary_folder (); 144 | files = list_entries (dir_path); 145 | generate_entry_list (self, dir_path, files); 146 | 147 | g_free (dir_path); 148 | g_ptr_array_unref (files); 149 | } 150 | 151 | static void 152 | entry_list_class_init (EntryListClass *klass) 153 | { 154 | GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); 155 | 156 | gtk_widget_class_set_template_from_resource (widget_class, "/com/bjareholt/johan/simple-diary/ui/entry_list.ui"); 157 | gtk_widget_class_bind_template_child (widget_class, EntryList, entry_list_box); 158 | 159 | entry_list_signals [SIGNAL_SELECTION_CHANGED] = 160 | g_signal_new ("selection-changed", G_TYPE_FROM_CLASS (klass), 161 | 0, 0, NULL, NULL, NULL, G_TYPE_NONE, 1, DIARY_TYPE_ENTRY); 162 | } 163 | 164 | typedef struct { 165 | gboolean found; 166 | gchar *filename; 167 | } EntryFindCtx; 168 | 169 | static void 170 | is_entry (GtkListBoxRow *row, EntryFindCtx *ctx) 171 | { 172 | Entry *entry; 173 | gchar *filename; 174 | EntryListing *listing; 175 | if (ctx->found) 176 | return; 177 | 178 | listing = DIARY_ENTRY_LISTING (gtk_list_box_row_get_child (row)); 179 | 180 | entry = entry_listing_get_entry (listing); 181 | g_object_get (entry, "filename", &filename, NULL); 182 | if (g_strcmp0 (filename, ctx->filename) == 0) { 183 | ctx->found = TRUE; 184 | } 185 | g_free (filename); 186 | } 187 | 188 | GtkListBoxRow * 189 | entry_list_find (EntryList *self, gchar *filename) 190 | { 191 | EntryFindCtx ctx; 192 | ctx.found = FALSE; 193 | ctx.filename = filename; 194 | GtkListBoxRow *row = NULL; 195 | 196 | /* TODO: Fix ugly loop */ 197 | gint i = 0; 198 | do { 199 | row = gtk_list_box_get_row_at_index (self->entry_list_box, i); 200 | if (row == NULL) { 201 | break; 202 | } 203 | is_entry (GTK_LIST_BOX_ROW (row), &ctx); 204 | if (ctx.found) { 205 | break; 206 | } 207 | i++; 208 | } while (TRUE); 209 | 210 | return row; 211 | } 212 | 213 | void 214 | entry_list_unfocus (EntryList *self) 215 | { 216 | gtk_list_box_select_row (self->entry_list_box, NULL); 217 | } 218 | 219 | static void 220 | entry_list_init (EntryList *self) 221 | { 222 | gtk_widget_init_template (GTK_WIDGET (self)); 223 | 224 | load_entry_list (self); 225 | 226 | gtk_list_box_set_sort_func (self->entry_list_box, list_box_sort, NULL, NULL); 227 | 228 | gtk_widget_set_vexpand (GTK_WIDGET (self), TRUE); 229 | gtk_widget_set_hexpand (GTK_WIDGET (self), TRUE); 230 | } 231 | 232 | GtkWidget * 233 | entry_list_new (void) 234 | { 235 | GtkWidget *entry_list = g_object_new (DIARY_TYPE_ENTRY_LIST, NULL); 236 | return entry_list; 237 | } 238 | 239 | gboolean 240 | entry_list_remove (EntryList *self, Entry *entry) 241 | { 242 | gboolean ret = FALSE; 243 | GtkListBoxRow *row; 244 | gchar *filename; 245 | 246 | g_object_get (entry, "filename", &filename, NULL); 247 | 248 | row = entry_list_find (self, filename); 249 | if (!row) { 250 | goto no_match; 251 | } 252 | 253 | gtk_list_box_remove (self->entry_list_box, GTK_WIDGET (row)); 254 | 255 | ret = TRUE; 256 | 257 | no_match: 258 | g_free (filename); 259 | return ret; 260 | } 261 | 262 | void 263 | entry_list_focus (EntryList *self, GtkListBoxRow *row) 264 | { 265 | GtkWidget *child; 266 | EntryListing *listing; 267 | Entry *entry; 268 | 269 | gtk_list_box_select_row (self->entry_list_box, GTK_LIST_BOX_ROW (row)); 270 | 271 | child = gtk_list_box_row_get_child (row); 272 | listing = DIARY_ENTRY_LISTING (child); 273 | entry = entry_listing_get_entry (listing); 274 | g_signal_emit_by_name (self, "selection-changed", entry); 275 | } 276 | -------------------------------------------------------------------------------- /src/widgets/entry_list.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "entry.h" 6 | #include "entry_listing.h" 7 | 8 | #define DIARY_TYPE_ENTRY_LIST (entry_list_get_type()) 9 | 10 | /* TODO GTK4: GtkScrolledWindow */ 11 | G_DECLARE_FINAL_TYPE (EntryList, entry_list, DIARY, ENTRY_LIST, GtkWindow) 12 | 13 | GtkWidget * entry_list_new (void); 14 | 15 | void entry_list_add_entry (EntryList *self, Entry *entry); 16 | void entry_list_focus (EntryList *self, GtkListBoxRow *row); 17 | void entry_list_unfocus (EntryList *self); 18 | GtkListBoxRow * entry_list_find (EntryList *self, gchar *filename); 19 | gboolean entry_list_remove (EntryList *self, Entry *entry); 20 | -------------------------------------------------------------------------------- /src/widgets/entry_listing.c: -------------------------------------------------------------------------------- 1 | #include "entry_listing.h" 2 | #include "entry_view.h" 3 | #include "window.h" 4 | #include "entry.h" 5 | 6 | struct _EntryListing 7 | { 8 | GtkBox parent_instance; 9 | 10 | GtkLabel *name_label; 11 | 12 | Entry *entry; 13 | }; 14 | 15 | G_DEFINE_TYPE (EntryListing, entry_listing, GTK_TYPE_BOX); 16 | 17 | static void 18 | entry_listing_class_init (EntryListingClass *klass) 19 | { 20 | GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); 21 | 22 | gtk_widget_class_set_template_from_resource (widget_class, "/com/bjareholt/johan/simple-diary/ui/entry_listing.ui"); 23 | gtk_widget_class_bind_template_child (widget_class, EntryListing, name_label); 24 | } 25 | 26 | static void 27 | entry_listing_init (EntryListing *self) 28 | { 29 | gtk_widget_init_template (GTK_WIDGET (self)); 30 | 31 | self->entry = NULL; 32 | } 33 | 34 | Entry * 35 | entry_listing_get_entry (EntryListing *listing) 36 | { 37 | return listing->entry; 38 | } 39 | 40 | static void 41 | entry_listing_update_title (EntryListing *self) 42 | { 43 | gchar *title; 44 | 45 | g_object_get (self->entry, "basename", &title, NULL); 46 | gtk_label_set_label(self->name_label, title); 47 | g_free (title); 48 | } 49 | 50 | void 51 | on_entry_filename_changed (Entry *entry, GParamSpecString *filename, gpointer user_data) 52 | { 53 | EntryListing *listing = DIARY_ENTRY_LISTING (user_data); 54 | 55 | g_print ("test\n"); 56 | 57 | entry_listing_update_title (listing); 58 | } 59 | 60 | /* 61 | void 62 | entry_deleted (Entry *entry, 63 | EntryListing *listing) 64 | { 65 | gtk_widget_destroy (GTK_WIDGET (listing)); 66 | } 67 | */ 68 | 69 | GtkWidget * 70 | entry_listing_new (Entry *entry) 71 | { 72 | EntryListing *listing = g_object_new (DIARY_TYPE_ENTRY_LISTING, NULL); 73 | listing->entry = entry; 74 | 75 | entry_listing_update_title (listing); 76 | 77 | /* TODO GTK4: Why was this needed before? */ 78 | //g_signal_connect (entry, "deleted", G_CALLBACK (entry_deleted), listing); 79 | 80 | g_signal_connect (entry, "notify::filename", G_CALLBACK (on_entry_filename_changed), listing); 81 | 82 | return GTK_WIDGET (listing); 83 | } 84 | -------------------------------------------------------------------------------- /src/widgets/entry_listing.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "entry.h" 6 | 7 | #define DIARY_TYPE_ENTRY_LISTING (entry_listing_get_type()) 8 | 9 | G_DECLARE_FINAL_TYPE (EntryListing, entry_listing, DIARY, ENTRY_LISTING, GtkBox) 10 | 11 | Entry * 12 | entry_listing_get_entry (EntryListing *listing); 13 | 14 | GtkWidget * 15 | entry_listing_new (Entry *entry); 16 | -------------------------------------------------------------------------------- /src/widgets/entry_name_dialog.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "widgets/entry_name_dialog.h" 4 | 5 | struct _EntryNameDialog 6 | { 7 | GtkWindow parent_instance; 8 | 9 | /* widgets */ 10 | GtkEntry *name_entry; 11 | GtkButton *button_cancel; 12 | GtkButton *button_ok; 13 | GtkButton *date_button; 14 | GtkLabel *date_label; 15 | GtkPopover *date_popover; 16 | GtkCalendar *date_calendar; 17 | }; 18 | 19 | struct _EntryNameDialogClass 20 | { 21 | GtkWindowClass parent_class; 22 | }; 23 | 24 | G_DEFINE_TYPE (EntryNameDialog, entry_name_dialog, GTK_TYPE_WINDOW); 25 | 26 | static void 27 | entry_name_dialog_class_init (EntryNameDialogClass *klass) 28 | { 29 | GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); 30 | 31 | gtk_widget_class_set_template_from_resource (widget_class, 32 | "/com/bjareholt/johan/simple-diary/ui/entry_name_dialog.ui"); 33 | gtk_widget_class_bind_template_child (widget_class, EntryNameDialog, 34 | name_entry); 35 | gtk_widget_class_bind_template_child (widget_class, EntryNameDialog, 36 | button_cancel); 37 | gtk_widget_class_bind_template_child (widget_class, EntryNameDialog, 38 | button_ok); 39 | gtk_widget_class_bind_template_child (widget_class, EntryNameDialog, 40 | date_button); 41 | gtk_widget_class_bind_template_child (widget_class, EntryNameDialog, 42 | date_label); 43 | gtk_widget_class_bind_template_child (widget_class, EntryNameDialog, 44 | date_popover); 45 | gtk_widget_class_bind_template_child (widget_class, EntryNameDialog, 46 | date_calendar); 47 | } 48 | 49 | static void 50 | date_button_clicked (GtkEntry *entry, gpointer user_data) 51 | { 52 | EntryNameDialog *dialog = DIARY_ENTRY_NAME_DIALOG (user_data); 53 | gtk_widget_set_visible (GTK_WIDGET (dialog->date_popover), TRUE); 54 | } 55 | 56 | static void 57 | entry_name_dialog_update_date_label (EntryNameDialog *self) 58 | { 59 | gint day, month, year; 60 | gchar *date_str; 61 | 62 | g_object_get (self->date_calendar, "day", &day, "month", &month, 63 | "year", &year, NULL); 64 | date_str = g_strdup_printf ("%d-%02d-%02d", year, month+1, day); 65 | g_object_set (self->date_label, "label", date_str, NULL); 66 | 67 | g_free (date_str); 68 | } 69 | 70 | static void 71 | entry_name_dialog_init (EntryNameDialog *self) 72 | { 73 | gtk_widget_init_template (GTK_WIDGET (self)); 74 | 75 | entry_name_dialog_update_date_label (self); 76 | g_signal_connect (self->date_button, "clicked", 77 | G_CALLBACK (date_button_clicked), self); 78 | g_signal_connect_swapped (self->date_calendar, "day-selected", 79 | G_CALLBACK (entry_name_dialog_update_date_label), self); 80 | } 81 | 82 | GtkWidget * 83 | entry_name_dialog_new (void) 84 | { 85 | EntryNameDialog *entry_name_dialog; 86 | GtkEntryBuffer *buffer; 87 | 88 | entry_name_dialog = g_object_new (DIARY_TYPE_ENTRY_NAME_DIALOG, NULL); 89 | buffer = gtk_entry_buffer_new ("Title", -1); 90 | gtk_entry_set_buffer (entry_name_dialog->name_entry, buffer); 91 | 92 | return GTK_WIDGET (entry_name_dialog); 93 | } 94 | 95 | static void 96 | dialog_response (GtkEntry *entry, 97 | gpointer user_data) 98 | { 99 | GTask *task = G_TASK (user_data); 100 | EntryNameDialog *self = DIARY_ENTRY_NAME_DIALOG (g_task_get_task_data (task)); 101 | gchar *text = entry_name_dialog_get_name (self); 102 | g_task_return_pointer (task, text, g_free); 103 | } 104 | 105 | static void 106 | dialog_cancel (GtkEntry *entry, 107 | gpointer user_data) 108 | { 109 | GTask *task = G_TASK (user_data); 110 | g_task_return_pointer (task, NULL, g_free); 111 | } 112 | 113 | void 114 | entry_name_dialog_open (EntryNameDialog *self, 115 | GtkWindow *parent, 116 | GAsyncReadyCallback callback, 117 | gpointer user_data) 118 | { 119 | GTask *task; 120 | 121 | task = g_task_new (self, FALSE, callback, user_data); 122 | g_task_set_check_cancellable (task, FALSE); 123 | g_task_set_source_tag (task, entry_name_dialog_open); 124 | g_task_set_task_data (task, self, g_object_unref); 125 | 126 | g_signal_connect (self->name_entry, "activate", G_CALLBACK (dialog_response), task); 127 | g_signal_connect (self->button_ok, "clicked", G_CALLBACK (dialog_response), task); 128 | g_signal_connect (self->button_cancel, "clicked", G_CALLBACK (dialog_cancel), task); 129 | 130 | gtk_window_set_transient_for (GTK_WINDOW (self), parent); 131 | gtk_widget_set_visible (GTK_WIDGET (self), TRUE); 132 | } 133 | 134 | gchar * 135 | entry_name_dialog_open_finish (EntryNameDialog *self, 136 | GAsyncResult *result) 137 | { 138 | gtk_window_destroy (GTK_WINDOW (self)); 139 | 140 | return g_task_propagate_pointer (G_TASK (result), NULL); 141 | } 142 | 143 | gchar * 144 | entry_name_dialog_get_name (EntryNameDialog * name_dialog) 145 | { 146 | GtkEntryBuffer *buffer; 147 | const gchar *title; 148 | gchar *date; 149 | gchar *name; 150 | 151 | buffer = gtk_entry_get_buffer (name_dialog->name_entry); 152 | title = gtk_entry_buffer_get_text (buffer); 153 | 154 | g_object_get (name_dialog->date_label, "label", &date, NULL); 155 | 156 | name = g_strdup_printf ("%s - %s", date, title); 157 | 158 | g_free (date); 159 | 160 | return name; 161 | } 162 | -------------------------------------------------------------------------------- /src/widgets/entry_name_dialog.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "entry.h" 6 | 7 | G_BEGIN_DECLS 8 | 9 | #define DIARY_TYPE_ENTRY_NAME_DIALOG (entry_name_dialog_get_type()) 10 | 11 | #define DIARY_TYPE_IS_ENTRY_NAME_DIALOG (object) \ 12 | (G_OBJECT_TYPE (object) == DIARY_TYPE_ENTRY_NAME_DIALOG) 13 | 14 | G_DECLARE_FINAL_TYPE (EntryNameDialog, entry_name_dialog, DIARY, ENTRY_NAME_DIALOG, GtkWindow) 15 | 16 | GtkWidget * 17 | entry_name_dialog_new (void); 18 | 19 | void 20 | entry_name_dialog_open (EntryNameDialog *self, 21 | GtkWindow *parent, 22 | GAsyncReadyCallback callback, 23 | gpointer user_data); 24 | 25 | gchar * 26 | entry_name_dialog_open_finish (EntryNameDialog *self, 27 | GAsyncResult *result); 28 | 29 | gchar * 30 | entry_name_dialog_get_name (EntryNameDialog * name_dialog); 31 | 32 | G_END_DECLS 33 | -------------------------------------------------------------------------------- /src/widgets/entry_rename_dialog.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "widgets/entry_rename_dialog.h" 4 | 5 | struct _EntryRenameDialog 6 | { 7 | GtkWindow parent_instance; 8 | 9 | /* widgets */ 10 | GtkEntry *name_entry; 11 | GtkButton *button_cancel; 12 | GtkButton *button_ok; 13 | }; 14 | 15 | struct _EntryRenameDialogClass 16 | { 17 | GtkWindowClass parent_class; 18 | }; 19 | 20 | G_DEFINE_TYPE (EntryRenameDialog, entry_rename_dialog, GTK_TYPE_WINDOW); 21 | 22 | static void 23 | entry_rename_dialog_class_init (EntryRenameDialogClass *klass) 24 | { 25 | GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); 26 | 27 | gtk_widget_class_set_template_from_resource (widget_class, 28 | "/com/bjareholt/johan/simple-diary/ui/entry_rename_dialog.ui"); 29 | gtk_widget_class_bind_template_child (widget_class, EntryRenameDialog, 30 | name_entry); 31 | gtk_widget_class_bind_template_child (widget_class, EntryRenameDialog, 32 | button_cancel); 33 | gtk_widget_class_bind_template_child (widget_class, EntryRenameDialog, 34 | button_ok); 35 | } 36 | 37 | static void 38 | entry_rename_dialog_init (EntryRenameDialog *self) 39 | { 40 | gtk_widget_init_template (GTK_WIDGET (self)); 41 | } 42 | 43 | GtkWidget * 44 | entry_rename_dialog_new (const gchar * basename) 45 | { 46 | EntryRenameDialog *entry_rename_dialog; 47 | GtkEntryBuffer *buffer; 48 | 49 | entry_rename_dialog = g_object_new (DIARY_TYPE_ENTRY_RENAME_DIALOG, NULL); 50 | buffer = gtk_entry_buffer_new (basename, -1); 51 | gtk_entry_set_buffer (entry_rename_dialog->name_entry, buffer); 52 | 53 | return GTK_WIDGET (entry_rename_dialog); 54 | } 55 | 56 | static void 57 | dialog_response (GtkEntry *entry, 58 | gpointer user_data) 59 | { 60 | GTask *task = G_TASK (user_data); 61 | EntryRenameDialog *self = DIARY_ENTRY_RENAME_DIALOG (g_task_get_task_data (task)); 62 | gchar *text = entry_rename_dialog_get_name (self); 63 | g_task_return_pointer (task, text, g_free); 64 | } 65 | 66 | static void 67 | dialog_cancel (GtkEntry *entry, 68 | gpointer user_data) 69 | { 70 | GTask *task = G_TASK (user_data); 71 | g_task_return_pointer (task, NULL, g_free); 72 | } 73 | 74 | void 75 | entry_rename_dialog_open (EntryRenameDialog *self, 76 | GtkWindow *parent, 77 | GAsyncReadyCallback callback, 78 | gpointer user_data) 79 | { 80 | GTask *task; 81 | 82 | task = g_task_new (self, FALSE, callback, user_data); 83 | g_task_set_check_cancellable (task, FALSE); 84 | g_task_set_source_tag (task, entry_rename_dialog_open); 85 | g_task_set_task_data (task, self, g_object_unref); 86 | 87 | g_signal_connect (self->name_entry, "activate", G_CALLBACK (dialog_response), task); 88 | g_signal_connect (self->button_ok, "clicked", G_CALLBACK (dialog_response), task); 89 | g_signal_connect (self->button_cancel, "clicked", G_CALLBACK (dialog_cancel), task); 90 | 91 | gtk_window_set_transient_for (GTK_WINDOW (self), parent); 92 | gtk_widget_set_visible (GTK_WIDGET (self), TRUE); 93 | } 94 | 95 | gchar * 96 | entry_rename_dialog_open_finish (EntryRenameDialog *self, 97 | GAsyncResult *result) 98 | { 99 | gtk_window_destroy (GTK_WINDOW (self)); 100 | 101 | return g_task_propagate_pointer (G_TASK (result), NULL); 102 | } 103 | 104 | gchar * 105 | entry_rename_dialog_get_name (EntryRenameDialog * rename_dialog) 106 | { 107 | GtkEntryBuffer *buffer; 108 | const gchar *text; 109 | 110 | buffer = gtk_entry_get_buffer (rename_dialog->name_entry); 111 | text = gtk_entry_buffer_get_text (buffer); 112 | 113 | return g_strdup (text); 114 | } 115 | -------------------------------------------------------------------------------- /src/widgets/entry_rename_dialog.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "entry.h" 6 | 7 | G_BEGIN_DECLS 8 | 9 | #define DIARY_TYPE_ENTRY_RENAME_DIALOG (entry_rename_dialog_get_type()) 10 | 11 | #define DIARY_TYPE_IS_ENTRY_RENAME_DIALOG (object) \ 12 | (G_OBJECT_TYPE (object) == DIARY_TYPE_ENTRY_RENAME_DIALOG) 13 | 14 | G_DECLARE_FINAL_TYPE (EntryRenameDialog, entry_rename_dialog, DIARY, ENTRY_RENAME_DIALOG, GtkWindow) 15 | 16 | GtkWidget * 17 | entry_rename_dialog_new (const gchar * basename); 18 | 19 | void 20 | entry_rename_dialog_open (EntryRenameDialog *self, 21 | GtkWindow *parent, 22 | GAsyncReadyCallback callback, 23 | gpointer user_data); 24 | 25 | gchar * 26 | entry_rename_dialog_open_finish (EntryRenameDialog *self, 27 | GAsyncResult *result); 28 | 29 | gchar * 30 | entry_rename_dialog_get_name (EntryRenameDialog * rename_dialog); 31 | 32 | G_END_DECLS 33 | -------------------------------------------------------------------------------- /src/widgets/entry_view.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "entry_view.h" 4 | #include "window.h" 5 | #include "entry.h" 6 | #include "entry_edit.h" 7 | #include "entry_rename_dialog.h" 8 | #include "entry_delete_dialog.h" 9 | #include "settings.h" 10 | 11 | struct _EntryView 12 | { 13 | GtkBox parent_instance; 14 | 15 | GtkButton *edit_button; 16 | GtkButton *rename_button; 17 | GtkButton *delete_button; 18 | 19 | GtkWidget *md_view; 20 | GtkWidget *md_viewport; 21 | 22 | Entry *entry; 23 | }; 24 | 25 | enum 26 | { 27 | SIGNAL_DELETED, 28 | LAST_SIGNAL, 29 | }; 30 | 31 | static guint entry_view_signals[LAST_SIGNAL] = { 0 }; 32 | 33 | G_DEFINE_TYPE (EntryView, entry_view, GTK_TYPE_BOX); 34 | 35 | static void 36 | entry_view_class_init (EntryViewClass *klass) 37 | { 38 | GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); 39 | 40 | gtk_widget_class_set_template_from_resource (widget_class, 41 | "/com/bjareholt/johan/simple-diary/ui/entry_view.ui"); 42 | gtk_widget_class_bind_template_child (widget_class, EntryView, md_viewport); 43 | gtk_widget_class_bind_template_child (widget_class, EntryView, edit_button); 44 | gtk_widget_class_bind_template_child (widget_class, EntryView, rename_button); 45 | gtk_widget_class_bind_template_child (widget_class, EntryView, delete_button); 46 | 47 | entry_view_signals [SIGNAL_DELETED] = 48 | g_signal_new ("deleted", G_TYPE_FROM_CLASS (klass), 49 | 0, 0, NULL, NULL, NULL, G_TYPE_NONE, 1, DIARY_TYPE_ENTRY); 50 | } 51 | 52 | static gboolean 53 | edit_button_clicked (GtkButton *button, gpointer user_data) 54 | { 55 | GtkWidget *entry_edit; 56 | DiaryWindow *diary_window; 57 | EntryView *self = (EntryView *) user_data; 58 | 59 | diary_window = diary_window_get_instance (); 60 | 61 | entry_edit = entry_edit_new (self->entry); 62 | diary_window_push_view (diary_window, entry_edit); 63 | 64 | return FALSE; 65 | } 66 | 67 | void 68 | rename_cb (GObject *obj, 69 | GAsyncResult *res, 70 | gpointer user_data) 71 | { 72 | EntryRenameDialog *dialog = DIARY_ENTRY_RENAME_DIALOG (obj); 73 | Entry *entry = DIARY_ENTRY (user_data); 74 | 75 | g_autofree gchar *name = entry_rename_dialog_open_finish(dialog, res); 76 | if (name != NULL) { 77 | // TODO: check if name contains dots or slashes 78 | g_autofree gchar *name_with_extension = g_strdup_printf ("%s.md", name); 79 | g_print ("Renaming entry to '%s'\n", name_with_extension); 80 | entry_rename_file (entry, name_with_extension); 81 | } 82 | } 83 | 84 | static gboolean 85 | rename_button_clicked (GtkButton *button, gpointer user_data) 86 | { 87 | EntryView *self = (EntryView *) user_data; 88 | GtkWindow *window; 89 | GtkWidget *dialog; 90 | gchar *basename; 91 | 92 | window = GTK_WINDOW (diary_window_get_instance ()); 93 | g_object_get (self->entry, "basename", &basename, NULL); 94 | 95 | dialog = entry_rename_dialog_new (basename); 96 | 97 | entry_rename_dialog_open(DIARY_ENTRY_RENAME_DIALOG (dialog), window, rename_cb, self->entry); 98 | 99 | g_free (basename); 100 | 101 | return TRUE; 102 | } 103 | 104 | static void 105 | delete_cb (GObject *obj, 106 | GAsyncResult *res, 107 | gpointer user_data) 108 | { 109 | EntryDeleteDialog *dialog = DIARY_ENTRY_DELETE_DIALOG (obj); 110 | EntryView *entry_view = DIARY_ENTRY_VIEW (user_data); 111 | Entry *entry = entry_view->entry; 112 | 113 | gboolean delete = entry_delete_dialog_open_finish(dialog, res); 114 | if (delete) { 115 | /* At this point, entry_view will get deleted, so we need to save it to a 116 | * local variable and ref it before emitting deleted */ 117 | g_object_ref (entry); 118 | g_signal_emit_by_name (entry_view, "deleted", entry_view->entry); 119 | entry_delete (entry); 120 | g_object_unref (entry); 121 | } 122 | } 123 | 124 | static gboolean 125 | delete_button_clicked (GtkButton *button, gpointer user_data) 126 | { 127 | EntryView *self = (EntryView *) user_data; 128 | GtkWindow *window; 129 | GtkWidget *delete_dialog; 130 | 131 | window = GTK_WINDOW (diary_window_get_instance ()); 132 | 133 | delete_dialog = entry_delete_dialog_new (); 134 | 135 | entry_delete_dialog_open(DIARY_ENTRY_DELETE_DIALOG (delete_dialog), window, delete_cb, self); 136 | 137 | return TRUE; 138 | } 139 | 140 | static void 141 | entry_view_load_md (EntryView *self) 142 | { 143 | GError *err = NULL; 144 | gchar *text_md; 145 | gchar *folder; 146 | 147 | g_object_get (self->entry, "folder", &folder, NULL); 148 | 149 | text_md = entry_read (self->entry, &err); 150 | 151 | self->md_view = gtk_md_view_new (text_md, folder); 152 | 153 | gtk_viewport_set_child (GTK_VIEWPORT (self->md_viewport), self->md_view); 154 | 155 | g_free (text_md); 156 | g_free (folder); 157 | } 158 | 159 | static void 160 | entry_view_init (EntryView *self) 161 | { 162 | g_type_ensure (GTK_TYPE_MD_VIEW); 163 | 164 | gtk_widget_init_template (GTK_WIDGET (self)); 165 | 166 | self->entry = NULL; 167 | 168 | g_signal_connect (self->edit_button, "clicked", (GCallback) edit_button_clicked, self); 169 | g_signal_connect (self->rename_button, "clicked", (GCallback) rename_button_clicked, self); 170 | g_signal_connect (self->delete_button, "clicked", (GCallback) delete_button_clicked, self); 171 | 172 | // This is necessary so it's reloaded if you edit an entry and go back 173 | g_assert (g_signal_connect (self, "map", G_CALLBACK (entry_view_load_md), NULL) > 0); 174 | } 175 | 176 | GtkWidget * 177 | entry_view_new (Entry *entry) 178 | { 179 | EntryView *self; 180 | 181 | self = g_object_new (DIARY_TYPE_ENTRY_VIEW, NULL); 182 | self->entry = entry; 183 | 184 | return GTK_WIDGET (self); 185 | } 186 | -------------------------------------------------------------------------------- /src/widgets/entry_view.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "entry.h" 6 | 7 | #define DIARY_TYPE_ENTRY_VIEW (entry_view_get_type()) 8 | 9 | G_DECLARE_FINAL_TYPE (EntryView, entry_view, DIARY, ENTRY_VIEW, GtkBox) 10 | 11 | GtkWidget * 12 | entry_view_new (Entry *entry); 13 | -------------------------------------------------------------------------------- /src/widgets/image_picker.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "image_picker.h" 4 | #include "utils.h" 5 | #include "window.h" 6 | 7 | struct _ImagePicker 8 | { 9 | GtkWindow parent_instance; 10 | 11 | /* widgets */ 12 | GtkButton *add_button; 13 | GtkButton *cancel_button; 14 | 15 | GtkEntry *name_entry; 16 | 17 | GtkButton *file_button; 18 | GtkButton *clipboard_button; 19 | gchar *picked_image_path; 20 | 21 | GtkPicture *image_preview; 22 | GdkTexture *image_texture; 23 | 24 | /* arguments */ 25 | gchar *basename; 26 | void (*finished_cb)(gchar *image_name, gchar *image_path, gpointer user_data); 27 | gpointer user_data; 28 | }; 29 | 30 | G_DEFINE_TYPE (ImagePicker, image_picker_dialog, GTK_TYPE_WINDOW); 31 | 32 | static void 33 | image_picker_dialog_class_init (ImagePickerClass *klass) 34 | { 35 | GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); 36 | 37 | gtk_widget_class_set_template_from_resource (widget_class, 38 | "/com/bjareholt/johan/simple-diary/ui/image_picker.ui"); 39 | 40 | gtk_widget_class_bind_template_child (widget_class, ImagePicker, add_button); 41 | gtk_widget_class_bind_template_child (widget_class, ImagePicker, cancel_button); 42 | 43 | gtk_widget_class_bind_template_child (widget_class, ImagePicker, name_entry); 44 | 45 | gtk_widget_class_bind_template_child (widget_class, ImagePicker, file_button); 46 | gtk_widget_class_bind_template_child (widget_class, ImagePicker, clipboard_button); 47 | 48 | gtk_widget_class_bind_template_child (widget_class, ImagePicker, image_preview); 49 | 50 | } 51 | 52 | static void select_file (GtkWidget *button, ImagePicker *dialog); 53 | static void select_clipboard (GtkWidget *button, ImagePicker *dialog); 54 | 55 | static void 56 | image_picker_dialog_init (ImagePicker *self) 57 | { 58 | self->picked_image_path = NULL; 59 | 60 | gtk_widget_init_template (GTK_WIDGET (self)); 61 | 62 | g_signal_connect (self->file_button, "clicked", (GCallback) select_file, self); 63 | g_signal_connect (self->clipboard_button, "clicked", (GCallback) select_clipboard, self); 64 | } 65 | 66 | static void 67 | open_file_cb(GObject* source_object, GAsyncResult* res, gpointer user_data) 68 | { 69 | GtkFileDialog *file_dialog = GTK_FILE_DIALOG (source_object); 70 | ImagePicker *dialog = DIARY_IMAGE_PICKER_DIALOG (user_data); 71 | GdkPixbuf *pixbuf; 72 | GdkTexture *texture; 73 | g_autoptr(GFile) file = NULL; 74 | GError *err = NULL; 75 | 76 | file = gtk_file_dialog_open_finish (file_dialog, res, &err); 77 | if (file == NULL) { 78 | g_print("Failed to select file: %s\n", err->message); 79 | return; 80 | } 81 | 82 | pixbuf = gdk_pixbuf_new_from_file (g_file_peek_path (file), &err); 83 | if (pixbuf == NULL) { 84 | utils_error_dialog (GTK_WIDGET (dialog), "Failed to load image: %s\n", err->message); 85 | g_clear_error (&err); 86 | return; 87 | } 88 | 89 | texture = gdk_texture_new_for_pixbuf (pixbuf); 90 | if (dialog->image_texture != NULL) { 91 | g_object_unref (dialog->image_texture); 92 | } 93 | dialog->image_texture = texture; 94 | gtk_picture_set_paintable (dialog->image_preview, GDK_PAINTABLE (texture)); 95 | } 96 | 97 | /* Called when pressing "file_button" */ 98 | static void 99 | select_file (GtkWidget *button, ImagePicker *dialog) 100 | { 101 | GtkFileDialog *file_dialog; 102 | 103 | file_dialog = gtk_file_dialog_new(); 104 | gtk_file_dialog_open (file_dialog, 105 | GTK_WINDOW (dialog), 106 | NULL, 107 | open_file_cb, 108 | dialog); 109 | } 110 | 111 | static void 112 | select_clipboard_cb (GObject *source_object, GAsyncResult *result, gpointer user_data) { 113 | ImagePicker *dialog = DIARY_IMAGE_PICKER_DIALOG (user_data); 114 | GError *err = NULL; 115 | GdkClipboard *clipboard = GDK_CLIPBOARD (source_object); 116 | GdkTexture* texture; 117 | 118 | texture = gdk_clipboard_read_texture_finish (clipboard, result, &err); 119 | if (texture == NULL) { 120 | goto error; 121 | } 122 | 123 | if (dialog->image_texture) { 124 | g_object_unref (dialog->image_texture); 125 | } 126 | dialog->image_texture = texture; 127 | gtk_picture_set_paintable (dialog->image_preview, GDK_PAINTABLE (texture)); 128 | 129 | error: 130 | if (err != NULL) { 131 | utils_error_dialog (GTK_WIDGET (dialog), "Failed to save image: %s\n", err->message); 132 | g_clear_error (&err); 133 | } 134 | } 135 | 136 | static void 137 | select_clipboard (GtkWidget *button, ImagePicker *dialog) 138 | { 139 | GdkClipboard *clipboard; 140 | 141 | clipboard = gtk_widget_get_clipboard (GTK_WIDGET (dialog)); 142 | if (clipboard == NULL) { 143 | utils_error_dialog (GTK_WIDGET (dialog), "Failed to get clipboard"); 144 | return; 145 | } 146 | 147 | gdk_clipboard_read_texture_async (clipboard, NULL, select_clipboard_cb, dialog); 148 | } 149 | 150 | ImagePicker * 151 | image_picker_dialog_new(gchar *basename) 152 | { 153 | ImagePicker *dialog; 154 | 155 | dialog = g_object_new (DIARY_TYPE_IMAGE_PICKER_DIALOG, NULL); 156 | dialog->basename = g_strdup (basename); 157 | 158 | return dialog; 159 | } 160 | 161 | Image * 162 | image_new (gchar *name, gchar *path) 163 | { 164 | Image *image = g_malloc0 (sizeof (Image)); 165 | image->name = name; 166 | image->path = path; 167 | return image; 168 | } 169 | 170 | void 171 | image_free (Image *image) 172 | { 173 | g_free (image->name); 174 | g_free (image->path); 175 | g_free (image); 176 | } 177 | 178 | static Image * 179 | save_image (ImagePicker *dialog) 180 | { 181 | g_autofree gchar *target_dir_absolute = NULL; 182 | g_autofree gchar *target_dir_relative = NULL; 183 | g_autofree gchar *image_path_absolute = NULL; 184 | g_autofree gchar *image_path_relative = NULL; 185 | g_autofree gchar *image_name = NULL; 186 | GtkEntryBuffer *buffer; 187 | 188 | buffer = gtk_entry_get_buffer (dialog->name_entry); 189 | image_name = g_strdup (gtk_entry_buffer_get_text (buffer)); 190 | 191 | /* relative path */ 192 | g_print ("basename: %s\n", dialog->basename); 193 | target_dir_relative = utils_get_photos_folder (dialog->basename, FALSE); 194 | image_path_relative = g_strdup_printf ("%s/%s.png", target_dir_relative, image_name); 195 | 196 | /* absolute path */ 197 | target_dir_absolute = utils_get_photos_folder (dialog->basename, TRUE); 198 | image_path_absolute = g_strdup_printf ("%s/%s.png", target_dir_absolute, image_name); 199 | 200 | if (dialog->image_texture == NULL) { 201 | utils_error_dialog (GTK_WIDGET (dialog), "No image selected\n"); 202 | return NULL; 203 | } 204 | 205 | /* TODO: save as jpg instead of png 206 | * should be possible with gdk_texture_download together with gdkpixbuf somehow */ 207 | if (!gdk_texture_save_to_png (dialog->image_texture, image_path_absolute)) { 208 | utils_error_dialog (GTK_WIDGET (dialog), "Could not save image to png file\n"); 209 | return NULL; 210 | } 211 | 212 | return image_new (g_steal_pointer (&image_name), g_steal_pointer (&image_path_relative)); 213 | } 214 | 215 | static void 216 | dialog_response (GtkWidget *widget, 217 | gpointer user_data) 218 | { 219 | GTask *task = G_TASK (user_data); 220 | ImagePicker *dialog = DIARY_IMAGE_PICKER_DIALOG (g_task_get_source_object (task)); 221 | Image *image = save_image(dialog); 222 | if (image) { 223 | gtk_window_close (GTK_WINDOW (dialog)); 224 | g_task_return_pointer (task, image, (GDestroyNotify) image_free); 225 | } else { 226 | g_task_return_pointer (task, NULL, NULL); 227 | } 228 | } 229 | 230 | static void 231 | dialog_cancel (GtkWidget *widget, 232 | gpointer user_data) 233 | { 234 | GTask *task = G_TASK (user_data); 235 | ImagePicker *dialog = DIARY_IMAGE_PICKER_DIALOG (g_task_get_source_object (task)); 236 | gtk_window_close (GTK_WINDOW (dialog)); 237 | g_task_return_pointer (task, NULL, NULL); 238 | } 239 | 240 | void 241 | image_picker_dialog_open(ImagePicker *self, 242 | GtkWindow *parent, 243 | GAsyncReadyCallback callback, 244 | gpointer user_data) 245 | { 246 | GTask *task; 247 | 248 | task = g_task_new (self, FALSE, callback, user_data); 249 | g_task_set_check_cancellable (task, FALSE); 250 | g_task_set_source_tag (task, image_picker_dialog_open); 251 | g_task_set_task_data (task, user_data, NULL); 252 | 253 | g_signal_connect (self->name_entry, "activate", G_CALLBACK (dialog_response), task); 254 | g_signal_connect (self->add_button, "clicked", G_CALLBACK (dialog_response), task); 255 | g_signal_connect (self->cancel_button, "clicked", G_CALLBACK (dialog_cancel), task); 256 | 257 | gtk_window_set_transient_for (GTK_WINDOW (self), parent); 258 | gtk_widget_set_visible (GTK_WIDGET (self), TRUE); 259 | } 260 | 261 | Image * 262 | image_picker_dialog_open_finish (ImagePicker *self, 263 | GAsyncResult *result) 264 | { 265 | gtk_window_destroy (GTK_WINDOW (self)); 266 | 267 | return g_task_propagate_pointer (G_TASK (result), NULL); 268 | } 269 | -------------------------------------------------------------------------------- /src/widgets/image_picker.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | typedef struct _Image { 6 | gchar *name; 7 | gchar *path; 8 | } Image; 9 | 10 | Image * 11 | image_new (gchar *name, gchar *path); 12 | 13 | void 14 | image_free (Image *image); 15 | 16 | G_DEFINE_AUTOPTR_CLEANUP_FUNC(Image, image_free); 17 | 18 | #define DIARY_TYPE_IMAGE_PICKER_DIALOG (image_picker_dialog_get_type()) 19 | 20 | G_DECLARE_FINAL_TYPE (ImagePicker, image_picker_dialog, DIARY, IMAGE_PICKER_DIALOG, GtkWindow); 21 | 22 | ImagePicker * 23 | image_picker_dialog_new(gchar *basename); 24 | 25 | void 26 | image_picker_dialog_open(ImagePicker *self, 27 | GtkWindow *parent, 28 | GAsyncReadyCallback callback, 29 | gpointer user_data); 30 | 31 | Image * 32 | image_picker_dialog_open_finish (ImagePicker *self, 33 | GAsyncResult *result); 34 | -------------------------------------------------------------------------------- /src/widgets/settings_view.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "settings.h" 4 | #include "utils.h" 5 | #include "widgets/settings_view.h" 6 | 7 | struct _SettingsView 8 | { 9 | GtkBox parent_instance; 10 | 11 | /* widgets */ 12 | GtkToggleButton *color_scheme_light; 13 | GtkToggleButton *color_scheme_default; 14 | GtkToggleButton *color_scheme_dark; 15 | GtkListBoxRow *about_button; 16 | 17 | /* properties */ 18 | Entry *entry; 19 | }; 20 | 21 | G_DEFINE_TYPE (SettingsView, settings_view, GTK_TYPE_BOX); 22 | 23 | static gboolean 24 | color_scheme_button_toggled (GtkButton *button, gpointer user_data) 25 | { 26 | gboolean active; 27 | ColorScheme color_scheme = (ColorScheme) user_data; 28 | 29 | g_object_get (button, "active", &active, NULL); 30 | if (active) { 31 | settings_set_color_scheme (color_scheme); 32 | utils_apply_color_scheme (); 33 | } 34 | return FALSE; 35 | } 36 | 37 | gboolean 38 | about_button_pressed (GtkWidget *widget, gpointer user_data) 39 | { 40 | GdkPixbuf *icon_pixbuf; 41 | GdkTexture *icon_texture; 42 | GError *err = NULL; 43 | gchar *authors[2] = { "Johan Bjäreholt", NULL }; 44 | 45 | icon_pixbuf = gdk_pixbuf_new_from_resource ("/com/bjareholt/johan/simple-diary/icons/logo.svg", &err); 46 | if (icon_pixbuf == NULL) { 47 | g_printerr ("failed to load image: %s\n", err->message); 48 | g_clear_error (&err); 49 | return TRUE; 50 | } 51 | 52 | icon_texture = gdk_texture_new_for_pixbuf (icon_pixbuf); 53 | 54 | gtk_show_about_dialog (NULL, 55 | "program-name", "Simple Diary", 56 | "title", "About", 57 | "logo", icon_texture, 58 | "authors", &authors, 59 | "license-type", GTK_LICENSE_GPL_3_0, 60 | "website", "https://github.com/johan-bjareholt/simple-diary-gtk", 61 | NULL); 62 | return TRUE; 63 | } 64 | 65 | static void 66 | settings_view_class_init (SettingsViewClass *klass) 67 | { 68 | GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); 69 | 70 | gtk_widget_class_set_template_from_resource (widget_class, 71 | "/com/bjareholt/johan/simple-diary/ui/settings.ui"); 72 | gtk_widget_class_bind_template_child (widget_class, SettingsView, 73 | color_scheme_light); 74 | gtk_widget_class_bind_template_child (widget_class, SettingsView, 75 | color_scheme_default); 76 | gtk_widget_class_bind_template_child (widget_class, SettingsView, 77 | color_scheme_dark); 78 | gtk_widget_class_bind_template_child (widget_class, SettingsView, 79 | about_button); 80 | } 81 | 82 | static void 83 | settings_view_init (SettingsView *self) 84 | { 85 | ColorScheme color_scheme; 86 | 87 | gtk_widget_init_template (GTK_WIDGET (self)); 88 | 89 | color_scheme = settings_get_color_scheme (); 90 | switch (color_scheme) { 91 | case COLOR_SCHEME_LIGHT: 92 | g_object_set (self->color_scheme_light, "active", TRUE, NULL); 93 | break; 94 | case COLOR_SCHEME_DEFAULT: 95 | g_object_set (self->color_scheme_default, "active", TRUE, NULL); 96 | break; 97 | case COLOR_SCHEME_DARK: 98 | g_object_set (self->color_scheme_dark, "active", TRUE, NULL); 99 | break; 100 | } 101 | 102 | g_signal_connect (self->color_scheme_light, "toggled", 103 | (GCallback) color_scheme_button_toggled, (gpointer) COLOR_SCHEME_LIGHT); 104 | g_signal_connect (self->color_scheme_default, "toggled", 105 | (GCallback) color_scheme_button_toggled, (gpointer) COLOR_SCHEME_DEFAULT); 106 | g_signal_connect (self->color_scheme_dark, "toggled", 107 | (GCallback) color_scheme_button_toggled, (gpointer) COLOR_SCHEME_DARK); 108 | g_signal_connect (self->about_button, "activated", (GCallback) about_button_pressed, self); 109 | } 110 | 111 | GtkWidget * 112 | settings_view_new () 113 | { 114 | SettingsView *settings_view = g_object_new (DIARY_TYPE_SETTINGS_VIEW, NULL); 115 | 116 | return GTK_WIDGET (settings_view); 117 | } 118 | -------------------------------------------------------------------------------- /src/widgets/settings_view.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "entry.h" 6 | 7 | G_BEGIN_DECLS 8 | 9 | #define DIARY_TYPE_SETTINGS_VIEW (settings_view_get_type()) 10 | 11 | #define DIARY_TYPE_IS_SETTINGS_VIEW(object) \ 12 | (G_OBJECT_TYPE (object) == DIARY_TYPE_SETTINGS_VIEW) 13 | 14 | G_DECLARE_FINAL_TYPE (SettingsView, settings_view, DIARY, SETTINGS_VIEW, GtkBox) 15 | 16 | GtkWidget * 17 | settings_view_new (); 18 | 19 | G_END_DECLS 20 | -------------------------------------------------------------------------------- /src/window.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "headerbuttons.h" 4 | #include "utils.h" 5 | #include "window.h" 6 | #include "widgets/entry_edit.h" 7 | #include "widgets/entry_view.h" 8 | #include "widgets/entry_listing.h" 9 | #include "widgets/entry_browser.h" 10 | #include "widgets/settings_view.h" 11 | 12 | struct _DiaryWindow 13 | { 14 | AdwApplicationWindow parent_instance; 15 | 16 | /* widgets */ 17 | GtkStack *content_stack; 18 | GtkButton *new_button; 19 | GtkButton *back_button; 20 | GtkButton *settings_button; 21 | 22 | /* views */ 23 | GList *view_stack; 24 | }; 25 | 26 | G_DEFINE_TYPE (DiaryWindow, diary_window, ADW_TYPE_APPLICATION_WINDOW); 27 | 28 | static DiaryWindow *diary_window_instance = NULL; 29 | 30 | DiaryWindow * 31 | diary_window_get_instance (void) 32 | { 33 | g_assert (diary_window_instance != NULL); 34 | return diary_window_instance; 35 | } 36 | 37 | static void 38 | diary_window_finalize (GObject *obj) 39 | { 40 | DiaryWindow *self = DIARY_WINDOW(obj); 41 | 42 | while (self->view_stack != NULL) { 43 | diary_window_pop_view (self); 44 | } 45 | 46 | G_OBJECT_CLASS (diary_window_parent_class)->finalize (obj); 47 | } 48 | 49 | static void 50 | diary_window_class_init (DiaryWindowClass *klass) 51 | { 52 | GObjectClass *obj_class = G_OBJECT_CLASS (klass); 53 | GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); 54 | 55 | obj_class->finalize = diary_window_finalize; 56 | 57 | gtk_widget_class_set_template_from_resource (widget_class, "/com/bjareholt/johan/simple-diary/ui/window.ui"); 58 | gtk_widget_class_bind_template_child (widget_class, DiaryWindow, content_stack); 59 | gtk_widget_class_bind_template_child (widget_class, DiaryWindow, new_button); 60 | gtk_widget_class_bind_template_child (widget_class, DiaryWindow, back_button); 61 | gtk_widget_class_bind_template_child (widget_class, DiaryWindow, settings_button); 62 | } 63 | 64 | static gpointer 65 | g_list_pop (GList **list) 66 | { 67 | gpointer last_data = NULL; 68 | GList *last = g_list_last (*list); 69 | if (last != NULL) { 70 | last_data = last->data; 71 | *list = g_list_remove (*list, last_data); 72 | } 73 | return last_data; 74 | } 75 | 76 | static void 77 | diary_window_update_header_buttons(DiaryWindow *self, GtkWidget *new_view, GtkWidget *old_view) 78 | { 79 | HeaderButtonsControlInterface *iface; 80 | 81 | /* on leave */ 82 | if (old_view != NULL) { 83 | iface = g_type_interface_peek (G_OBJECT_GET_CLASS (old_view), DIARY_TYPE_HEADER_BUTTONS); 84 | if (iface) { 85 | iface->on_leave (old_view, self->new_button, self->back_button, self->settings_button); 86 | } else { 87 | header_buttons_control_default_on_leave (old_view, self->new_button, self->back_button, self->settings_button); 88 | } 89 | } 90 | 91 | /* on enter */ 92 | iface = g_type_interface_peek (G_OBJECT_GET_CLASS (new_view), DIARY_TYPE_HEADER_BUTTONS); 93 | if (iface) { 94 | iface->on_enter (new_view, self->new_button, self->back_button, self->settings_button); 95 | } else { 96 | header_buttons_control_default_on_enter (new_view, self->new_button, self->back_button, self->settings_button); 97 | } 98 | } 99 | 100 | void 101 | diary_window_push_view (DiaryWindow *self, GtkWidget *new_view) 102 | { 103 | GtkWidget *prev_view = NULL; 104 | if (self->view_stack != NULL) { 105 | prev_view = g_list_last (self->view_stack)->data; 106 | } 107 | /* add new view */ 108 | self->view_stack = g_list_append (self->view_stack, new_view); 109 | gtk_stack_add_child (self->content_stack, new_view); 110 | 111 | /* set new view in focus */ 112 | gtk_stack_set_visible_child (self->content_stack, new_view); 113 | 114 | /* Update header buttons */ 115 | diary_window_update_header_buttons (self, new_view, prev_view); 116 | } 117 | 118 | void 119 | diary_window_pop_view(DiaryWindow *self) 120 | { 121 | GtkWidget *current_view; 122 | GtkWidget *prev_view; 123 | GList *prev_view_list; 124 | 125 | current_view = g_list_pop (&self->view_stack); 126 | if (current_view == NULL) { 127 | return; 128 | } 129 | prev_view_list = g_list_last (self->view_stack); 130 | if (prev_view_list == NULL) { 131 | return; 132 | } 133 | prev_view = GTK_WIDGET (g_list_last (self->view_stack)->data); 134 | 135 | diary_window_update_header_buttons (self, prev_view, current_view); 136 | 137 | /* remove the current view */ 138 | gtk_stack_remove (self->content_stack, current_view); 139 | 140 | /* add previous view */ 141 | gtk_stack_set_visible_child (self->content_stack, prev_view); 142 | } 143 | 144 | gboolean 145 | back_button_pressed (GtkWidget *back_button, gpointer user_data) 146 | { 147 | DiaryWindow *self = DIARY_WINDOW (user_data); 148 | GtkWidget *widget = GTK_WIDGET (g_list_last (self->view_stack)->data); 149 | HeaderButtonsControlInterface *iface; 150 | 151 | iface = g_type_interface_peek (G_OBJECT_GET_CLASS (widget), DIARY_TYPE_HEADER_BUTTONS); 152 | if (iface) { 153 | iface->on_back_pressed (widget); 154 | } else { 155 | header_buttons_control_default_on_back_pressed (widget); 156 | } 157 | 158 | return FALSE; 159 | } 160 | 161 | gboolean 162 | new_button_pressed (GtkWidget *back_button, gpointer user_data) 163 | { 164 | DiaryWindow *self = DIARY_WINDOW (user_data); 165 | GtkWidget *widget = GTK_WIDGET (g_list_last (self->view_stack)->data); 166 | HeaderButtonsControlInterface *iface; 167 | 168 | iface = g_type_interface_peek (G_OBJECT_GET_CLASS (widget), DIARY_TYPE_HEADER_BUTTONS); 169 | if (iface) { 170 | iface->on_new_pressed (widget); 171 | } else { 172 | header_buttons_control_default_on_new_pressed (widget); 173 | } 174 | 175 | return FALSE; 176 | } 177 | 178 | gboolean 179 | settings_button_pressed (GtkWidget *widget, gpointer user_data) 180 | { 181 | DiaryWindow *diary_window = DIARY_WINDOW (user_data); 182 | GtkWidget *settings_view = settings_view_new (); 183 | diary_window_push_view (diary_window, settings_view); 184 | return FALSE; 185 | } 186 | 187 | static void 188 | diary_window_init (DiaryWindow *self) 189 | { 190 | gtk_widget_init_template (GTK_WIDGET (self)); 191 | 192 | diary_window_push_view (self, entry_browser_new ()); 193 | //diary_window_push_view (self, gtk_label_new ("testing 123")); 194 | 195 | g_signal_connect (self->new_button, "clicked", (GCallback) new_button_pressed, self); 196 | g_signal_connect (self->back_button, "clicked", (GCallback) back_button_pressed, self); 197 | g_signal_connect (self->settings_button, "clicked", (GCallback) settings_button_pressed, self); 198 | } 199 | 200 | GtkApplicationWindow * 201 | diary_window_new (GtkApplication *application) 202 | { 203 | GtkApplicationWindow *window; 204 | 205 | window = GTK_APPLICATION_WINDOW (g_object_new (DIARY_TYPE_WINDOW, "application", application, NULL)); 206 | g_assert (diary_window_instance == NULL); 207 | diary_window_instance = DIARY_WINDOW (window); 208 | 209 | return window; 210 | } 211 | -------------------------------------------------------------------------------- /src/window.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #define DIARY_TYPE_WINDOW (diary_window_get_type()) 7 | 8 | G_DECLARE_FINAL_TYPE (DiaryWindow, diary_window, DIARY, WINDOW, AdwApplicationWindow) 9 | 10 | DiaryWindow * 11 | diary_window_get_instance (void); 12 | 13 | void 14 | diary_window_push_view (DiaryWindow *self, GtkWidget *new_view); 15 | 16 | void 17 | diary_window_pop_view (DiaryWindow *self); 18 | 19 | GtkApplicationWindow * 20 | diary_window_new (GtkApplication *application); 21 | -------------------------------------------------------------------------------- /test/meson.build: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/johan-bjareholt/simple-diary-gtk/0e5c2296c041da9b2bc01bdb18f1faf4a15ba7ee/test/meson.build -------------------------------------------------------------------------------- /tools/book-generator/scripts/md2tex.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import sys 4 | import os 5 | import shutil 6 | 7 | import marko 8 | 9 | USAGE = f"{sys.argv[0]} md-dir tex-dir" 10 | 11 | if len(sys.argv) < 3: 12 | print("Not enough arguments") 13 | print(USAGE) 14 | exit(1) 15 | 16 | md_dir = sys.argv[1] 17 | tex_dir = sys.argv[2] 18 | 19 | try: 20 | os.makedirs(tex_dir) 21 | except FileExistsError: 22 | print(f"folder already exists: {tex_dir}") 23 | exit(1) 24 | 25 | def filepath_escape(string): 26 | #string = string.replace("\"", "\\\"") 27 | return string 28 | 29 | def latex_escape(string): 30 | # Following chars have special meaning in latex: 31 | # & % $ # _ { } ~ ^ \ 32 | string = string.replace("\\", "\\\\") 33 | string = string.replace("\"", "''") 34 | # TODO: Also do single quotes 35 | string = string.replace("%", "\%") 36 | string = string.replace("$", "\$") 37 | string = string.replace("#", "\#") 38 | string = string.replace("_", "\_") 39 | string = string.replace("{", "\{") 40 | string = string.replace("}", "\}") 41 | string = string.replace("&", "\\&") 42 | string = string.replace("~", "\~") 43 | string = string.replace("^", "\^") 44 | return string 45 | 46 | 47 | class Image: 48 | def __init__(self, path, title): 49 | self.path = path 50 | self.title = title 51 | 52 | class LatexRenderer(marko.renderer.Renderer): 53 | """The most common renderer for markdown parser""" 54 | def __init__(self): 55 | self.images = [] 56 | marko.renderer.Renderer.__init__(self) 57 | 58 | def render(self, element): # type: (Element) -> str 59 | # Store the root node to provide some context to render functions 60 | if not self.root_node: 61 | self.root_node = element # type: ignore 62 | if hasattr(element, "get_type"): 63 | func_name = "render_" + element.get_type(snake_case=True) 64 | render_func = getattr(self, func_name, None) 65 | if render_func is not None and ( 66 | getattr(render_func, "_force_delegate", False) or self.delegate 67 | ): 68 | return render_func(element) 69 | doc_str = self.render_children(element) 70 | 71 | doc_str += "\FloatBarrier\n" 72 | 73 | if len(self.images) > 0: 74 | for index, img in enumerate(self.images): 75 | if index % 2 == 0: 76 | doc_str += "\\begin{figure}[ht]\n" 77 | # set width and height limits for photos 78 | if index == len(self.images)-1 and index % 2 == 0: 79 | width = "\\textwidth" 80 | height = "0.45\\textheight" 81 | else: 82 | width = "0.5\\textwidth" 83 | height = "0.45\\textheight" 84 | # create photos folder if it does not already exist 85 | img_dir = os.path.dirname(img.path) 86 | try: 87 | os.makedirs(tex_dir + "/" + img_dir) 88 | except FileExistsError: 89 | pass 90 | # copy image file 91 | orig_file = f"{md_dir}/{img.path}" 92 | dest_file = f"{tex_dir}/{img.path}" 93 | shutil.copy2(orig_file, dest_file) 94 | # append tex 95 | doc_str += " \\begin{subfigure}{" + width + "}\n" 96 | doc_str += " \\hfill\n" 97 | doc_str += " \includegraphics[height=" + height + ",width=\\textwidth,keepaspectratio]{" + filepath_escape(img.path) + "}\\hspace{\\fill}\n" 98 | doc_str += " \subcaption{" + latex_escape(img.title) + "}\n" 99 | doc_str += " \end{subfigure}\n" 100 | if index % 2 == 1 or index == len(self.images)-1: 101 | doc_str += "\end{figure}\n" 102 | 103 | return doc_str 104 | 105 | def render_paragraph(self, element): 106 | children = self.render_children(element) 107 | if element._tight: 108 | return children 109 | else: 110 | return f"\n{children}\n" 111 | 112 | def render_list(self, element): 113 | if element.ordered: 114 | tag = "ol" 115 | extra = f' start="{element.start}"' if element.start != 1 else "" 116 | else: 117 | tag = "ul" 118 | extra = "" 119 | return "<{tag}{extra}>\n{children}\n".format( 120 | tag=tag, extra=extra, children=self.render_children(element) 121 | ) 122 | 123 | def render_list_item(self, element): 124 | if len(element.children) == 1 and getattr(element.children[0], "_tight", False): 125 | sep = "" 126 | else: 127 | sep = "\n" 128 | return f"
  • {sep}{self.render_children(element)}
  • \n" 129 | 130 | def render_quote(self, element): 131 | return f"
    \n{self.render_children(element)}
    \n" 132 | 133 | def render_fenced_code(self, element): 134 | lang = ( 135 | f' class="language-{element.lang}"' 136 | if element.lang 137 | else "" 138 | ) 139 | return "
    {}
    \n".format( 140 | lang, html.escape(element.children[0].children) 141 | ) 142 | 143 | def render_code_block(self, element): 144 | return self.render_fenced_code(element) 145 | 146 | def render_html_block(self, element): 147 | return element.children 148 | 149 | def render_thematic_break(self, element): 150 | return "
    \n" 151 | 152 | def render_heading(self, element): 153 | return "{children}\n".format( 154 | level=element.level, children=self.render_children(element) 155 | ) 156 | 157 | def render_setext_heading(self, element): 158 | return self.render_heading(element) 159 | 160 | def render_blank_line(self, element): 161 | return "" 162 | 163 | def render_link_ref_def(self, element): 164 | return "" 165 | 166 | def render_emphasis(self, element): 167 | return f"{self.render_children(element)}" 168 | 169 | def render_strong_emphasis(self, element): 170 | return f"{self.render_children(element)}" 171 | 172 | def render_inline_html(self, element): 173 | return self.render_html_block(element) 174 | 175 | def render_plain_text(self, element): 176 | if isinstance(element.children, str): 177 | return element.children 178 | return self.render_children(element) 179 | 180 | def render_link(self, element): 181 | template = '{}' 182 | title = ( 183 | f' title="{element.title}"' 184 | if element.title 185 | else "" 186 | ) 187 | url = element.dest 188 | body = self.render_children(element) 189 | return template.format(url, title, body) 190 | 191 | def render_auto_link(self, element): 192 | return self.render_link(element) 193 | 194 | def render_image(self, element): 195 | render_func = self.render 196 | self.render = self.render_plain_text 197 | body = self.render_children(element) 198 | self.render = render_func 199 | 200 | self.images.append(Image(element.dest, latex_escape(body))) 201 | return "" 202 | 203 | def render_literal(self, element): 204 | return self.render_raw_text(element) 205 | 206 | def render_raw_text(self, element): 207 | return latex_escape(element.children) 208 | 209 | def render_line_break(self, element): 210 | if element.soft: 211 | return "\n" 212 | return "
    \n" 213 | 214 | def render_code_span(self, element): 215 | return element.children 216 | #return f"{html.escape(element.children)}" 217 | 218 | 219 | def latex2md(md_path): 220 | title = os.path.basename(md_path)[:-3] 221 | print(f"converting '{title}'") 222 | tex_path = f"{tex_dir}/{title}.tex" 223 | with open(md_path, "r") as md_file: 224 | tex_str = "" 225 | tex_str += "\section*{" + latex_escape(title) + "}\n" 226 | md_str = md_file.read() 227 | converter = marko.Markdown(renderer=LatexRenderer) 228 | tex_str += converter.convert(md_str) 229 | with open(tex_path, "w") as tex_file: 230 | tex_file.write(tex_str) 231 | return title 232 | 233 | if os.path.isdir(md_dir): 234 | entries = [] 235 | md_file_paths = os.listdir(md_dir) 236 | for md_path in md_file_paths: 237 | if not md_path.endswith(".md"): 238 | continue 239 | md_path_full = f"{md_dir}/{md_path}" 240 | entry_name = latex2md(md_path_full) 241 | entries.append(entry_name) 242 | 243 | root_str = "" 244 | root_str += "\documentclass[a4paper,12pt]{article}\n" 245 | root_str += "\n" 246 | # International characters 247 | root_str += "\\usepackage[utf8]{inputenc}\n" 248 | # Font family 249 | root_str += "\\usepackage[T1]{fontenc}\n" 250 | root_str += "\\usepackage{charter}\n" 251 | # 252 | root_str += "\\usepackage{graphicx}\n" 253 | root_str += "\\usepackage[space]{grffile}\n" 254 | root_str += "\\usepackage[labelformat=empty]{caption}\n" 255 | root_str += "\\usepackage[labelformat=empty]{subcaption}\n" 256 | # 2cm margins 257 | root_str += "\\usepackage{geometry}\n" 258 | root_str += "\\geometry{margin=2cm}\n" 259 | # Allow more floats 260 | root_str += "\\extrafloats{1000}\n" 261 | # No newline after sections 262 | root_str += "\\usepackage{newclude}\n" 263 | # Float barriers 264 | root_str += "\\usepackage[section]{placeins}\n" 265 | root_str += "\n" 266 | root_str += "\\begin{document}\n" 267 | root_str += "\n" 268 | 269 | for entry_name in sorted(entries): 270 | #root_str += "\\include{" + entry_name + "}\n" 271 | root_str += "\\include*{" + entry_name + "}\n" 272 | root_str += "\n" 273 | root_str += "\end{document}\n" 274 | with open(f"{tex_dir}/diary.tex", "w") as diary_file: 275 | diary_file.write(root_str) 276 | else: 277 | if not md_dir.endswith(".md"): 278 | print("file has to end with .md") 279 | exit(1) 280 | latex2md(md_dir) 281 | --------------------------------------------------------------------------------