// swad_alert.c: alerts /* SWAD (Shared Workspace At a Distance), is a web platform developed at the University of Granada (Spain), and used to support university teaching. This file is part of SWAD core. Copyright (C) 1999-2019 Antonio Caņas Vargas This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ /*****************************************************************************/ /********************************* Headers ***********************************/ /*****************************************************************************/ #define _GNU_SOURCE // For vasprintf #include // For NULL #include // For va_start, va_end #include // For FILE, fprintf, vasprintf #include // For free #include "swad_alert.h" #include "swad_form.h" #include "swad_global.h" /*****************************************************************************/ /************** External global variables from others modules ****************/ /*****************************************************************************/ extern struct Globals Gbl; /*****************************************************************************/ /****************************** Public constants *****************************/ /*****************************************************************************/ /*****************************************************************************/ /***************************** Private constants *****************************/ /*****************************************************************************/ static const char *Ale_AlertIcons[Ale_NUM_ALERT_TYPES] = { NULL, // Ale_NONE "clipboard.svg", // Ale_CLIPBOARD "info-circle.svg", // Ale_INFO "check-circle.svg", // Ale_SUCCESS "question64x64.gif", // Ale_QUESTION animated gif "warning64x64.gif", // Ale_WARNING animated gif "error64x64.gif", // Ale_ERROR animated gif }; /*****************************************************************************/ /******************************* Private types *******************************/ /*****************************************************************************/ /*****************************************************************************/ /***************************** Private prototypes ****************************/ /*****************************************************************************/ static void Ale_ShowFixAlert (Ale_AlertType_t AlertType,const char *Txt); static void Ale_ShowFixAlertAndButton1 (Ale_AlertType_t AlertType,const char *Txt); /*****************************************************************************/ /*************************** Reset delayed alert *****************************/ /*****************************************************************************/ void Ale_ResetDelayedAlert (void) { Gbl.DelayedAlert.Type = Ale_NONE; // Reset alert Gbl.DelayedAlert.Section = NULL; Gbl.DelayedAlert.Txt[0] = '\0'; } /*****************************************************************************/ /******************** Show an alert message to the user **********************/ /*****************************************************************************/ void Ale_ShowDelayedAlert (void) { if (Gbl.DelayedAlert.Type != Ale_NONE) Ale_ShowFixAlert (Gbl.DelayedAlert.Type,Gbl.DelayedAlert.Txt); Ale_ResetDelayedAlert (); } void Ale_ShowAlert (Ale_AlertType_t AlertType,const char *fmt,...) { va_list ap; int NumBytesPrinted; char *Txt; if (AlertType != Ale_NONE) { /***** Print format and list of variables into text *****/ va_start (ap,fmt); NumBytesPrinted = vasprintf (&Txt,fmt,ap); va_end (ap); if (NumBytesPrinted < 0) // If memory allocation wasn't possible, // or some other error occurs, // vasprintf will return -1 Lay_NotEnoughMemoryExit (); /***** Show alert *****/ Ale_ShowFixAlert (AlertType,Txt); /***** Free text *****/ free ((void *) Txt); } } static void Ale_ShowFixAlert (Ale_AlertType_t AlertType,const char *Txt) { if (AlertType != Ale_NONE) { /****** Print fix alert and button ******/ Ale_ShowFixAlertAndButton1 (AlertType,Txt); Ale_ShowAlertAndButton2 (ActUnk,NULL,NULL, NULL,Btn_NO_BUTTON,NULL); } } void Ale_ShowAlertAndButton (Act_Action_t NextAction,const char *Anchor,const char *OnSubmit, void (*FuncParams) (), Btn_Button_t Button,const char *TxtButton, Ale_AlertType_t AlertType,const char *fmt,...) { va_list ap; int NumBytesPrinted; char *Txt; /***** Print format and list of variables into text *****/ va_start (ap,fmt); NumBytesPrinted = vasprintf (&Txt,fmt,ap); va_end (ap); if (NumBytesPrinted < 0) // If memory allocation wasn't possible, // or some other error occurs, // vasprintf will return -1 Lay_NotEnoughMemoryExit (); /****** Print fix alert and button ******/ Ale_ShowFixAlertAndButton1 (AlertType,Txt); Ale_ShowAlertAndButton2 (NextAction,Anchor,OnSubmit, FuncParams,Button,TxtButton); /***** Free text *****/ free ((void *) Txt); } void Ale_ShowAlertAndButton1 (Ale_AlertType_t AlertType,const char *fmt,...) { va_list ap; int NumBytesPrinted; char *Txt; /***** Print format and list of variables into text *****/ va_start (ap,fmt); NumBytesPrinted = vasprintf (&Txt,fmt,ap); va_end (ap); if (NumBytesPrinted < 0) // If memory allocation wasn't possible, // or some other error occurs, // vasprintf will return -1 Lay_NotEnoughMemoryExit (); /****** Print start of fix alert and button ******/ Ale_ShowFixAlertAndButton1 (AlertType,Txt); /***** Free text *****/ free ((void *) Txt); } static void Ale_ShowFixAlertAndButton1 (Ale_AlertType_t AlertType,const char *Txt) { extern const char *Txt_Close; char IdAlert[Frm_MAX_BYTES_ID + 1]; static const bool AlertClosable[Ale_NUM_ALERT_TYPES] = { false, // Ale_NONE true, // Ale_CLIPBOARD true, // Ale_INFO true, // Ale_SUCCESS true, // Ale_QUESTION true, // Ale_WARNING true, // Ale_ERROR }; /****** If start of page is not written yet, do it now ******/ if (!Gbl.Layout.HTMLStartWritten) Lay_WriteStartOfPage (); /***** Start box *****/ fprintf (Gbl.F.Out,"" "
"); /***** Icon to close the alert *****/ if (AlertClosable[AlertType]) fprintf (Gbl.F.Out,"
" "" "\"%s\"" "" "
", IdAlert, Gbl.Prefs.URLIcons, Txt_Close,Txt_Close); /***** Write message *****/ fprintf (Gbl.F.Out,"
%s" "
", Txt); } void Ale_ShowAlertAndButton2 (Act_Action_t NextAction,const char *Anchor,const char *OnSubmit, void (*FuncParams) (), Btn_Button_t Button,const char *TxtButton) { /***** Optional button *****/ if (NextAction != ActUnk && Button != Btn_NO_BUTTON && TxtButton) if (TxtButton[0]) { /* Start form */ Frm_StartFormAnchor (NextAction,Anchor); Frm_StartFormAnchorOnSubmit (NextAction,Anchor,OnSubmit); if (FuncParams) FuncParams (); /* Put button *****/ Btn_PutButton (Button,TxtButton); /* End form */ Frm_EndForm (); } /***** End box *****/ fprintf (Gbl.F.Out,"
" ""); } /*****************************************************************************/ /*** Show alert when user not found or no permission to perform an action ****/ /*****************************************************************************/ void Acc_ShowAlertUserNotFoundOrYouDoNotHavePermission (void) { extern const char *Txt_User_not_found_or_you_do_not_have_permission_; Ale_ShowAlert (Ale_WARNING,Txt_User_not_found_or_you_do_not_have_permission_); }